gpt4 book ai didi

php - 如何在教义2中添加正确的一对多关系?

转载 作者:行者123 更新时间:2023-11-29 22:41:43 25 4
gpt4 key购买 nike

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

object(Doctrine\ORM\PersistentCollection)

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

Undefined index: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;
}

}

仅供引用:将 Like 更改为 Like 或其他,因为 mysql 关键字 LIKE

关于php - 如何在教义2中添加正确的一对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29316632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com