简体   繁体   English

如何在 Doctrine 2 中正确添加一对多关系?

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

I have 2 files(tables) in my Entity, and want to join Like table to Comment table.I used one to many so can connect Comment.id to Like.comment_id and can get comments likes with one selection.When I doing this and dumping $comment->getLikes() I'm getting object of this type我的实体中有 2 个文件(表),并且想将Like表加入到Comment表中。我使用了一对多所以可以将Comment.id连接到Like.comment_id并且可以通过一个选择获得评论喜欢。当我这样做时倾销$comment->getLikes()我正在获取这种类型的对象

object(Doctrine\\ORM\\PersistentCollection)对象(Doctrine\\ORM\\PersistentCollection)

When try to do something like this $comment->getLikes()->first() getting当尝试做这样的事情时$comment->getLikes()->first()得到

Undefined index:commentId未定义的索引:commentId

So I cant get likes from database, am I doing something wrong?And if it is possible explain why its working such way?所以我不能从数据库中得到喜欢,我做错了什么吗?如果有可能解释为什么它会这样工作? Here is comment Entity.这是评论实体。

<?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;
    }

}

an here is Like Entity这里就像实体

<?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;
    }

}

FYI : Change like to likes or others because mysql keyword LIKE仅供参考:将喜欢更改为喜欢或其他,因为 mysql 关键字 LIKE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM