【问题标题】:Symfony entity set array as single entity elementsSymfony 实体集数组作为单个实体元素
【发布时间】:2016-06-16 08:04:00
【问题描述】:

我有问题,我想将原型数组添加到数据库,但这显示了这个错误:

“AppBundle\Entity\Tag”类型的预期参数,给定“数组”

...

Post ->setTag (array(array('value' => 'test'), array('value' => 'tess')))

这是我的标签设置器:

public function setTag(\AppBundle\Entity\Tag $tag = null)
{
    $this->tag = $tag;

    return $this;
}

我有两个实体有关系,这里是关系:

class Post
{
    /**
     * @ORM\ManyToMany(targetEntity="Tag", inversedBy="post")
     * @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
     */
    private $tag;

    public function setTag(\AppBundle\Entity\Tag $tag = null)
    {
        $this->tag = $tag;

        return $this;
    }
}

和标签:

class Tag
{
    /**
     * @ORM\ManyToMany(targetEntity="Post", mappedBy="tag")
     */
    private $post;
}

来源:

http://snipet.co.uk/kR

http://snipet.co.uk/gcf

http://snipet.co.uk/0VI

【问题讨论】:

    标签: arrays collections entity symfony


    【解决方案1】:

    您正在尝试为 Post 和 Tag 之间的双向多对多关系建模。

    因此,首先,您的 getter 需要返回一组对象,而您的 setter 需要接受一组对象 - 不仅仅是代码中的一个对象(您的 setTag 方法接受 Tag 类型的参数 -但你需要一个类似数组的参数)。

    其次,Doctrine 框架不适用于简单的 PHP 数组,而是使用 \Doctrine\Common\Collections\Collection 的实现。

    接下来,您需要使用 Collection 类的实现在实体类的构造函数中初始化集合字段 - 您可以使用 \Doctrine\Common\Collections\ArrayCollection。

    所以你的实体类应该看起来像这样:

    /**
    * @ORM\Entity
    */
    class Post
    {
        /**
        * @ORM\ManyToMany(targetEntity="Tag", inversedBy="posts")
        * @ORM\JoinTable(name="posts_tags")
        */
        private $tags;
    
        public function __construct()
        {
            $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        public function getTags()
        {
            return $this->tags;
        }
    
        public function setTags(\Doctrine\Common\Collections\Collection $tags)
        {
            $this->tags = $tags;
        }
    }
    
    
    /**
    * @ORM\Entity
    */
    class Tag
    {
        /**
        * @ORM\ManyToMany(targetEntity="Post", mappedBy="tags")
        */
        private $posts;
    
        public function __construct()
        {
            $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        public function getPosts()
        {
            return $this->posts;
        }
    
        public function setPosts(\Doctrine\Common\Collections\Collection $posts)
        {
            $this->posts = $posts;
        }
    
    }
    

    我强烈建议您再次阅读 Doctrine 框架的文档,如何注释您的实体以及如何建模关系:http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html

    【讨论】:

    • 好的,我在这里更改了我的代码是最新的,我用你的提示来做。 snipet.co.uk/qXZ/raw 但总是在提交时从我看到消息:“类型为“AppBundle\Entity\Tag”的预期参数,给定“字符串””也许我的控制器坏了?
    • 是的,有些东西用错误类型的参数调用了您的 addTag 或 removeTag 方法。如果我上面的回答对您有帮助,您能否点个赞(旁边的向上箭头按钮)?谢谢
    猜你喜欢
    • 2021-03-26
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2019-08-31
    • 2011-10-10
    相关资源
    最近更新 更多