如何在条令2中适当添加一对多关系


How to add properly one-to-many relationship in Doctrine 2?

我的实体中有两个文件(表),希望将Like表连接到Comment表。我使用了一对多,所以可以将Comment.id连接到Like.comment_id,并且可以通过一个选择获得评论点赞。当我这样做并转储$comment->getLikes()时,我得到的是这种类型的对象

对象(条令''ORM''持久收集)

当尝试执行类似$comment->getLikes()->first()获取的操作时

未定义的索引: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;
    }
}
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

,将like改为likes或others