使用其他实体的形式(Symfony 5)

问题描述 投票:0回答:1

我使用Symfony 5,我想创建一个表单。我使用数据库连接学说。我要为我的表单创建一个,此选择的选项是数据库的其他表中的列。

我有两个实体:

// 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()
    ]);
}

现在当我转到新帖子页面时,此错误显示:

可捕获的致命错误:App \ Entity \ MagTermTaxonomy类的对象无法转换为字符串

如何解决此问题?

forms symfony doctrine-orm doctrine symfony5
1个回答
1
投票

将__toString()添加到您的MagPosts.php

public function __toString() {
    return $this->name
}

并编辑您的表单代码

->add('name', EntityType::class, [
    'class' => MagTermTaxonomy::class,
    'choice_label' => 'name'
    'mapped' => false
])
© www.soinside.com 2019 - 2024. All rights reserved.