Всем привет, на сайте в админке при созданий поста в поле где надо выбрать категорию выходит такая ошибка, точнее страница вообще не отображается,:
Если в форме убрать поле категорий то все отображается.
Код формы
<?php
namespace App\Form;
use App\Entity\Blog;
use App\Entity\Category;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BlogType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', null, [
'attr' => ['autofocus' => true],
])
->add('content', null)
->add('img', FileType::class)
->add('publishedAt', DateType::class)
->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name',
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Blog::class,
]);
}
}
Entity Blog
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\BlogRepository")
*/
class Blog
{
const NUM_ITEMS = 5;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank(message="Пожалуйста, загрузите картинку в формате JPG или PNG.")
* @Assert\File(mimeTypes={ "application/jpg", "application/png" })
*/
private $img;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="posts")
* @ORM\JoinColumn(nullable=true)
*/
private $category;
/**
* @ORM\Column(type="string")
*/
private $slug;
/**
* @ORM\Column(type="integer")
*/
private $views;
/**
* @ORM\Column(type="datetime")
*/
private $publishedAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* @param mixed $title
*/
public function setTitle($title): void
{
$this->title = $title;
}
/**
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* @param mixed $content
*/
public function setContent($content): void
{
$this->content = $content;
}
/**
* @return mixed
*/
public function getImg()
{
return $this->img;
}
/**
* @param mixed $img
*/
public function setImg($img): void
{
$this->img = $img;
}
/**
* @return mixed
*/
public function getCategory(): Category
{
return $this->category;
}
/**
* @param mixed $category
*/
public function setCategory(Category $category): void
{
$this->category = $category;
}
/**
* @return mixed
*/
public function getSlug()
{
return $this->slug;
}
/**
* @param mixed $slug
*/
public function setSlug($slug): void
{
$this->slug = $slug;
}
/**
* @return mixed
*/
public function getViews()
{
return $this->views;
}
/**
* @param mixed $views
*/
public function setViews($views): void
{
$this->views = $views;
}
/**
* @return mixed
*/
public function getPublishedAt()
{
return $this->publishedAt;
}
/**
* @param mixed $publishedAt
*/
public function setPublishedAt($publishedAt): void
{
$this->publishedAt = $publishedAt;
}
public function getAuthor(): User
{
return $this->author;
}
public function setAuthor(User $author): void
{
$this->author = $author;
}
}
Entity category
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Blog", mappedBy="category")
*/
private $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
public function __toString()
{
return $this->name;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
/**
* @return Collection|Post[]
*/
public function getPosts()
{
return $this->posts;
}
}
BlogController
<?php
namespace App\Controller\Admin;
use App\Entity\Blog;
use App\Form\BlogType;
use App\Utils\Slugger;
use App\Repository\BlogRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* @Route("/admin/blog")
*/
class BlogController extends Controller
{
/**
* @Route("/", name="admin_blog")
*/
public function index(BlogRepository $posts): Response
{
$authorPosts = $posts->findBy(['author' => $this->getUser()], ['publishedAt' => 'DESC']);
return $this->render('admin/blog/index.html.twig', [
'posts' => $authorPosts,
]);
}
/**
* @Route("/new", name="admin_post_new")
*/
public function new(Request $request): Response
{
$post = new Blog();
$post->setAuthor($this->getUser());
$form = $this->createForm(BlogType::class, $post);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$post->setSlug(Slugger::slugify($post->getTitle()));
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
$this->addFlash('success', 'post.created_successfully');
return $this->redirectToRoute('admin_post_new');
}
return $this->render('admin/blog/new.html.twig', [
'post' => $post,
'form' => $form->createView(),
]);
}
}