Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Doctrine orm Symfony:多对多更新_Doctrine Orm_Symfony_Symfony Forms - Fatal编程技术网

Doctrine orm Symfony:多对多更新

Doctrine orm Symfony:多对多更新,doctrine-orm,symfony,symfony-forms,Doctrine Orm,Symfony,Symfony Forms,我拥有具有多对多关系的Post和Tag实体。在Postcreate and edit表单中有一个文本框,我可以在其中输入与该帖子相关的标记,标记之间用逗号分隔。例如,当我为标题为“Post1”的帖子输入tag1、tag2、tag3时,表单将创建帖子和标记实体,并将这些标记添加到该帖子的标记列表中。我使用data transformer创建标记实体 class Post{ /** * @ORM\ManyToMany(targetEntity="Tag", mappedBy="po

我拥有具有多对多关系的
Post
Tag
实体。在
Post
create and edit表单中有一个文本框,我可以在其中输入与该帖子相关的标记,标记之间用逗号分隔。例如,当我为标题为“Post1”的帖子输入tag1、tag2、tag3时,表单将创建帖子和标记实体,并将这些标记添加到该帖子的标记列表中。我使用data transformer创建标记实体

class Post{
    /**
     * @ORM\ManyToMany(targetEntity="Tag", mappedBy="posts",cascade={"all"})
     */ 
    protected $tags;

    public function __construct() {
            $this->tags = new ArrayCollection();
        }

    /**
     * @return ArrayCollection
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * @param Tag $tag
     */
    public function addTag(Tag $tag)
    {
        $tag->addPost($this);

        $this->tags->add($tag);
    }

    /**
     * @param Tag $tag
     */
    public function removeTag(Tag $tag)
    {
        $this->tags->removeElement($tag);
    }
}
PostType

 public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('title', TextType::class, array('label' => 'Title'))
        ->add('tags', TextType::class, array('label' => 'Tags'))
    ;

    $builder->get('tags')
        ->addModelTransformer(new TagViewTransformer($this->manager));
}
TagViewTransformer

class TagViewTransformer implements DataTransformerInterface
{

public function transform($value)
{
    /...
}

public function reverseTransform($value)
{
    $tags = array();
    if ( $value )
    {
        if( strpos($value, ',') !== false )
        {
            $list = array_unique(explode(",", $value));
        }
        else
        {
            $list = array(trim($value));
        }

        foreach ( $list AS $tagName )
        {
            $tag = $this->em
                ->getRepository('CoreBundle:Tag')
                ->findOneBy(array('name' => trim($tagName)));

            if( !$tag )
            {
                $tag = new Tag();
                $tag->setName(trim($tagName));
                $this->em->persist($tag);
            }

            $tags[] = $tag;
        }
     }
    return $tags;
    }
}
当我尝试创建Post时,所有标记都转换为实体,并添加到Post的标记列表中。但当我尝试编辑时,我开始遇到问题

 public function editAction(Request $request, Post $post)
{
    $deleteForm = $this->createDeleteForm($post);
    $editForm = $this->createForm(PostType::class, $post);
    $editForm->handleRequest($request);

    $originalTags = $post->getTags();

    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $newTags = $editForm->get('tags')->getData();

        foreach ($originalTags as $currentTag) {
            if (!in_array($currentTag, $newTags)) {
                $post->removeTag($currentTag);
            }
        }

        $em->persist($post);
        $em->flush();

        return $this->redirectToRoute('post_show', array('id' => $post->getId()));
    }

    return $this->render('AppBundle:Post:edit.html.twig', array(
        'entity' => $post,
        'form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}
假设Post有标签:
tag1、tag2、tag3
,但我想删除
tag3
并添加
tag4
。因此,我将标签文本框更改为
tag1、tag2、tag4
。然而,当我提交表单时,我得到了
tag1、tag2、tag3、tag4
。所以tag3并没有从Post的标签列表中删除

editAction代码有什么问题?

试试这个

public function editAction(Request $request, Post $post)
{
    $deleteForm = $this->createDeleteForm($post);
    $editForm = $this->createForm(PostType::class, $post);
    $editForm->handleRequest($request);



    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($post);
        $em->flush();

        return $this->redirectToRoute('post_show', array('id' => $post->getId()));
    }

    return $this->render('AppBundle:Post:edit.html.twig', array(
        'entity' => $post,
        'form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}
用于执行此操作:

class Post
{
    /**
     * @ORM\ManyToMany(targetEntity="Tag", mappedBy="posts", cascade={"all"}, orphanRemoval=true)
     */ 
    protected $tags;

    /* Rest of your class */

}
做一些测试,可能这里不需要所有的级联