【问题标题】:How to add properly one-to-many relationship in Doctrine 2?如何在 Doctrine 2 中正确添加一对多关系?
【发布时间】:2015-03-28 11:32:42
【问题描述】:

我的实体中有 2 个文件(表),并且想将 Like 表加入到 Comment 表中。我使用了一对多,因此可以将 Comment.id 连接到 Like.comment_id 并且可以获得 cmets 喜欢的一个选择。当我这样做并转储$comment->getLikes() 时,我得到了这种类型的对象

对象(学说\ORM\PersistentCollection)

当尝试做这样的事情时$comment->getLikes()->first()getting

未定义索引:commentId

所以我无法从数据库中获得喜欢,我做错了什么吗?如果可以解释为什么它会这样工作? 这里是评论实体。

<?php

namespace App\Entity;

use App\Entity;
use Doctrine\ORM\Mapping;

/**
 * @Entity
 * @Table(name="comments")
 */
class Comment extends Entity
{
    /**
     *
     * /**
     * @Column(type="string", length=255)
     * @var string
     */
    protected $url;


    /**
     * @Column(type="string", length=255)
     * @var string
     */
    protected $description;

    /**
     * @Column(name="userId", type="integer")
     * @var int
     */
    protected $userId;

    /**
     * @Column(name="lng", type="float")
     * @var float
     */
    protected $lng;

    /**
     * @Column(name="lat", type="float")
     * @var float
     */
    protected $lat;


    /**
     * @Column(type="string", length=255)
     * @var string
     */
    protected $tags;


    /**
     * @var Like
     * @OneToMany(targetEntity="Like", mappedBy="commentId")
     **/
    protected $likes;


    /**
     * @return string
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * @param string $url
     */
    public function setUrl($url)
    {
        $this->url = $url;
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * @return int
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * @param int $userId
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;
    }

    /**
     * @return float
     */
    public function getLong()
    {
        return $this->lng;
    }

    /**
     * @param float $lng
     */
    public function setLong($lng)
    {
        $this->lng = $lng;
    }

    /**
     * @return float
     */
    public function getLat()
    {
        return $this->lat;
    }

    /**
     * @param float $lat
     */
    public function setLat($lat)
    {
        $this->lat = $lat;
    }

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

    /**
     * @param string $tags
     */
    public function setTags($tags)
    {
        $this->tags = $tags;
    }

    /**
     * @return array()
     */
    public function getLikes()
    {
        return $this->likes;
    }

}

这里就像实体

<?php

namespace App\Entity;

use App\Entity;
use Doctrine\ORM\Mapping;

/**
 * @Entity
 * @Table(name="like")
 */
class Like extends Entity
{
    /**
     * @Column(name="commentId", type="integer")
     * @var int
     */
    protected $commentId;

    /**
     * @Column(name="userId", type="integer")
     * @var int
     */
    protected $userId;


    /**
     * @return int
     */
    public function getCommentId()
    {
        return $this->commentId;
    }

    /**
     * @param int $commentId
     */
    public function setCommentId($commentId)
    {
        $this->commentId = $commentId;
    }

    /**
     * @return int
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * @param int $userId
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;
    }

}

【问题讨论】:

    标签: php mysql orm doctrine-orm one-to-many


    【解决方案1】:
    class Comment
    {
        /**
         * @Id
         * @Column(type="integer")
         * @GeneratedValue
         */
        private $id;
    
        /**
         * @Column(type="string", length=255)
         * @var string
         */
        protected $url;
    
    
        /**
         * @Column(type="string", length=255)
         * @var string
         */
        protected $description;
    
        /**
         * @Column(name="userId", type="integer")
         * @var int
         */
        protected $userId;
    
        /**
         * @Column(name="lng", type="float")
         * @var float
         */
        protected $lng;
    
        /**
         * @Column(name="lat", type="float")
         * @var float
         */
        protected $lat;
    
    
        /**
         * @Column(type="string", length=255)
         * @var string
         */
        protected $tags;
    
        /**
         * @OneToMany(targetEntity="Like", mappedBy="comment")
         */
        protected $likes;
    
        public function __construct()
        {
            $this->likes = new ArrayCollection();
        }
    
    
        /**
         * @return string
         */
        public function getUrl()
        {
            return $this->url;
        }
    
        /**
         * @param string $url
         */
        public function setUrl($url)
        {
            $this->url = $url;
        }
    
        /**
         * @return string
         */
        public function getDescription()
        {
            return $this->description;
        }
    
        /**
         * @param string $description
         */
        public function setDescription($description)
        {
            $this->description = $description;
        }
    
        /**
         * @return int
         */
        public function getUserId()
        {
            return $this->userId;
        }
    
        /**
         * @param int $userId
         */
        public function setUserId($userId)
        {
            $this->userId = $userId;
        }
    
        /**
         * @return float
         */
        public function getLong()
        {
            return $this->lng;
        }
    
        /**
         * @param float $lng
         */
        public function setLong($lng)
        {
            $this->lng = $lng;
        }
    
        /**
         * @return float
         */
        public function getLat()
        {
            return $this->lat;
        }
    
        /**
         * @param float $lat
         */
        public function setLat($lat)
        {
            $this->lat = $lat;
        }
    
        /**
         * @return string
         */
        public function getTags()
        {
            return $this->tags;
        }
    
        /**
         * @param string $tags
         */
        public function setTags($tags)
        {
            $this->tags = $tags;
        }
    
        /**
         * @param Like $like
         */
        public function addLike(Like $like)
        {
            $this->likes->add($like);
            $like->setComment($this);
        }
    
        /**
         * @return ArrayCollection
         */
        public function getLikes()
        {
            return $this->likes;
        }
    }
    
    /**
     * @Entity
     * @Table(name="likes")
     */
    class Like
    {
        /**
         * @Id
         * @Column(type="integer")
         * @GeneratedValue
         */
        private $id;
    
        /**
         * @ManyToOne(targetEntity="Comment", inversedBy="likes")
         */
        protected $comment;
    
        /**
         * @param mixed $comment
         */
        public function setComment(Comment $comment)
        {
            $this->comment = $comment;
        }
    
        /**
         * @Column(name="userId", type="integer")
         */
        protected $userId;
    
        /**
         * @return int
         */
        public function getUserId()
        {
            return $this->userId;
        }
    
        /**
         * @param int $userId
         */
        public function setUserId($userId)
        {
            $this->userId = $userId;
        }
    
    }
    

    仅供参考:因为 mysql 关键字 LIKE 将喜欢更改为喜欢或其他人

    【讨论】:

    • 你能解释一下我需要改变什么吗?
    • 好的,我这样做了,但是当我这样做时 $comment->getLikes() 我得到了 object(Doctrine\ORM\PersistentCollection)[128] private 'snapshot' => array (size=0 ) empty private 'owner' => 相同的对象,但我已经在表中手动添加,所以他们不能 ba epmty,:(
    • 您能解释一下这种情况下的问题吗? getLikes() 返回所有与评论相关的点赞。谢谢
    • 当我转储 id 16 的评论变量时,我得到了这个 pix.toile-libre.org/upload/original/1427699889.png,因为你看到喜欢的是 Doctrine\ORM\PersistentCollection 并且为空,但我添加了喜欢 pix.toile-libre.org/upload/original/1427700010.png
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    相关资源
    最近更新 更多