Я использую Symfony 5 и хочу создать форму. Я использую базу данных, соединяющуюся с doctrine. Я хочу создать для моей формы и параметры этого выбора это столбцы в другой таблице базы данных.
У меня есть две сущности:
// src/Entity/MagPosts.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\MagPostsRepository")
*/
class MagPosts
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $title;
/**
* @ORM\Column(type="string", length=4000000000)
*/
private $content;
/**
* @ORM\Column(type="datetime")
*/
private $date;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $status;
/**
* @ORM\Column(type="datetime")
*/
private $modified;
/**
* @ORM\Column(type="bigint", nullable=true)
*/
private $comment_count = 0;
/**
* @ORM\Column(type="string")
*/
private $imageFilename;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getModified(): ?\DateTimeInterface
{
return $this->modified;
}
public function setModified(\DateTimeInterface $modified): self
{
$this->modified = $modified;
return $this;
}
public function getCommentCount(): ?string
{
return $this->comment_count;
}
public function setCommentCount(?string $comment_count): self
{
$this->comment_count = $comment_count;
return $this;
}
public function getImageFilename(): ?string
{
return $this->imageFilename;
}
public function setImageFilename(string $imageFilename): self
{
$this->imageFilename = $imageFilename;
return $this;
}
}
И:
// src/Entity/MagPosts.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\MagTermTaxonomyRepository")
*/
class MagTermTaxonomy
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $taxonomy;
/**
* @ORM\Column(type="string", length=1000)
*/
private $slug;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTaxonomy(): ?string
{
return $this->taxonomy;
}
public function setTaxonomy(string $taxonomy): self
{
$this->taxonomy = $taxonomy;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}
Теперь я создаю форму:
// src/Form/MagNewPostType.php
<?php
namespace App\Form;
use App\Entity\MagPosts;
use App\Entity\MagTermTaxonomy;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
class MagNewPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => false
])
->add('imageFile', FileType::class, [
'label' => false,
'mapped' => false,
'required' => false,
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'image/jpeg',
'image/png',
],
'mimeTypesMessage' => 'Please upload a valid image file',
])
],
])
->add('content', TextareaType::class, [
'label' => false
])
->add('name', EntityType::class, [
'class' => MagTermTaxonomy::class,
'query_builder' => function(EntityRepository $entityRepository){
return $entityRepository->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
'mapped' => false
])
->add('slug', EntityType::class, [
'class' => MagTermTaxonomy::class,
'query_builder' => function(EntityRepository $entityRepository){
return $entityRepository->createQueryBuilder('u')
->orderBy('u.slug', 'ASC');
},
'mapped' => false
])
->add('tags', TextType::class, [
'mapped' => false
])
->add('status', CheckboxType::class, [
'label' => 'Status',
'required' => false
])
->add('save', SubmitType::class, ['label' => 'save'])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => MagPosts::class,
]);
}
}
И мой контроллер:
/**
* @Route("/newPost", name="new_post")
*/
public function magNew(EntityManagerInterface $entityManager, Request $request)
{
$magPosts = new MagPosts();
$form = $this->createForm(MagNewPostType::class, $magPosts);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$magPosts = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($magPosts);
$entityManager->flush();
return $this->redirectToRoute('home_page');
}
return $this->render('post/newpost.html.twig', [
'newPostForm' => $form->createView()
]);
}
Теперь, когда я go на новую страницу сообщения, эта ошибка отображается:
Catchable Fatal Error: Объект класса App \ Entity \ MagTermTaxonomy не может быть преобразован в строку
Как я могу исправить эту проблему?