Symfony实体属性未更新


Symfony entity property not updating

我有一个小问题。

我有一篇带有可重复属性标记的文章。当我编辑现有文章并添加新标签时,一切都可以。但如果从文章中的表单中删除标签,这个标签仍然存在。

据我所知,发布数据绑定到表单和实体。这就是新标记在材质中出现的方式。

但为什么它们在绑定新的帖子数据后没有消失呢?

   public function editAction(Article $article, Request $request)
    {
        // The second parameter is used to specify on what object the role is tested.
        if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
            return $this->redirectToRoute('login');
        }
        // update Article modify date
        $article->setModifyDate(new 'DateTime());
        // create form
        $form = $this->createForm(ArticleType::class, $article, [
            'manager' => $this->getDoctrine()->getManager()
        ]);
        $form->handleRequest($request);
        //print_r($article->getTags()); Deleted tag still here

更新

这是我的文章实体

**
 * Class Article
 * @package BackendBundle'Entity
 * @ORM'Table(name="articles")
 * @ORM'Entity(repositoryClass="BackendBundle'Entity'ArticleRepository")
 */
class Article
{
    /**
     * @ORM'Column(type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM'Column(type="string", length=255, unique=true)
     * @Assert'NotBlank()
     */
    private $name;
    /**
     * @ORM'Column(type="string", length=60, unique=true)
     * @Assert'NotBlank()
     */
    private $alias;
    /**
     * @ORM'Column(type="text")
     * @Assert'NotBlank()
     */
    private $content;
    /**
     * @ORM'Column(type="integer", length=255)
     * @Assert'NotBlank()
     */
    private $author;
    /**
     * @ORM'Column(type="datetime")
     * @Assert'NotBlank()
     */
    private $publishDate;
    /**
     * @ORM'Column(type="datetime")
     * @Assert'NotBlank()
     */
    private $modifyDate;
    /**
     * @ORM'ManyToMany(targetEntity="Tag", inversedBy="articles", cascade={"persist","remove"})
     * @ORM'JoinTable(
     *     name="articles_tags",
     *     joinColumns={@ORM'JoinColumn(name="article_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM'JoinColumn(name="tag_id", referencedColumnName="id")})
     */
    private $tags;
    /**
     * Article constructor.
     */
    public function __construct()
    {
        $this->tags = new ArrayCollection();
    }
    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set article name.
     *
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
    /**
     * Return article name.
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Set articles alias.
     *
     * @param string $alias
     *
     * @return $this
     */
    public function setAlias($alias)
    {
        $this->alias = $alias;
        return $this;
    }
    /**
     * Returns article alias.
     * @return mixed
     */
    public function getAlias()
    {
        return $this->alias;
    }
    /**
     * Set article content.
     *
     * @param string $content
     *
     * @return $this
     */
    public function setContent($content)
    {
        $this->content = $content;
        return $this;
    }
    /**
     * Return article content
     * @return string
     */
    public function getContent()
    {
        return $this->content;
    }
    /**
     * Set article author
     *
     * @param integer $author ID of author
     *
     * @return mixed
     */
    public function setAuthor($author)
    {
        $this->author = $author;
        return $this;
    }
    /**
     * Return author id.
     * @return int Id of the author.
     */
    public function getAuthor()
    {
        return $this->author;
    }
    /**
     * Set article publish date.
     *
     * @param string $date
     *
     * @return $this
     */
    public function setPublishDate($date)
    {
        $this->publishDate = $date;
        return $this;
    }
    /**
     * Return article publish date.
     * @return string
     */
    public function getPublishDate()
    {
        return $this->publishDate;
    }
    /**
     * Set article modify date
     *
     * @param string $date
     *
     * @return $this
     */
    public function setModifyDate($date)
    {
        $this->modifyDate = $date;
        return $this;
    }
    /**
     * Return article modify date
     * @return string
     */
    public function getModifyDate()
    {
        return $this->modifyDate;
    }
    /**
     * Set tags for article.
     *
     * @param  ArrayCollection|array $tags
     *
     * @return $this
     */
    public function setTags($tags)
    {
        if (!empty($tags))
        {
            foreach ($tags as $tag)
            {
                $this->setTag($tag);
            }
        }
        return $this;
    }
    /**
     * Set article tag.
     *
     * @param Tag $tag
     *
     * @return $this
     *
     */
    public function setTag(Tag $tag)
    {
        if (!$this->tags->contains($tag))
        {
            $this->tags->set($tag->getName(), $tag);
            $tag->setArticle($this);
        }
        return $this;
    }
    /**
     * Remove all tags from article.
     */
    public function removeTags()
    {
        if(!empty($this->tags))
        {
            foreach($this->tags as $tag)
            {
                $this->removeTag($tag);
            }
        }
        return $this;
    }
    /**
     * @param 'BackendBundle'Entity'Tag $tag
     *
     * @return $this
     */
    public function removeTag(Tag $tag)
    {
        if ($this->tags->contains($tag))
        {
            $this->tags->removeElement($tag);
            $tag->removeArticle($this);
        }
        return $this;
    }
    /**
     * Returns all tags for article.
     * @return ArrayCollection
     */
    public function getTags()
    {
        return $this->tags;
    }
}

文章类型

/**
 * Class ArticleType
 * @package AppBundle'Form
 */
class ArticleType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $tagTransformer = new TagTransformer($options['manager']);
        $builder
            ->add('name', TextType::class, array(
                'label_attr' => array('class' => ''),
                'attr'       => array('class' => 'form-control'),
                'label'      => 'article.name'
            ))
            ->add('alias', TextType::class, array(
                'label_attr' => array('class' => ''),
                'attr'       => array('class' => 'form-control'),
                'label'      => 'article.alias'
            ))
            ->add('content', TextareaType::class, array(
                'label_attr' => array('class' => ''),
                'attr'       => array(
                    'class' => 'form-control'
                ),
                'label'      => 'article.content'
            ))
            ->add('publish_date', DateType::class, array(
                'label_attr' => array('class' => ''),
                'attr'       => array('class' => 'form-control'),
                'format'     => 'dd-MM-yyyy',
                'label'      => 'article.publish_date'
            ))
            ->add('modify_date', DateType::class, array(
                'label_attr' => array('class' => ''),
                'attr'       => array('class' => 'form-control'),
                'format'     => 'dd-MM-yyyy',
                'label'      => 'article.modify_date'
            ))
            ->add($builder->create('tags', CollectionType::class, array(
                'label'        => 'article.tags',
                'by_reference' => false,
                'allow_add'    => true,
                'allow_delete' => true,
            ))->addViewTransformer($tagTransformer));
    }
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BackendBundle'Entity'Article',
        ));
        $resolver->setRequired(array(
            'manager',
        ));
        $resolver->setAllowedTypes('manager', 'Doctrine'Common'Persistence'ObjectManager');
    }
}

尝试将Article实体中的add tag和remove tag函数修改为类似的函数

public function addTag(Tag $tag)
{
    $this->tags[] = $tag;
    $tag->addArticle($this);
    return $this;
}
public function removeTag(Tag $tag)
{
    $this->tags->removeElement($tag);
    $tag->removeArticle($this);
}