php - How to add properly one-to-many relationship in Doctrine 2? -
i have 2 files(tables) in entity, , want join like
table comment
table.i used 1 many can connect comment.id
like.comment_id
, can comments likes 1 selection.when doing , dumping $comment->getlikes()
i'm getting object of type
object(doctrine\orm\persistentcollection)
when try $comment->getlikes()->first()
getting
undefined index:commentid
so cant likes database, doing wrong?and if possible explain why working such way? here 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 * @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 entity
<?php namespace app\entity; use app\entity; use doctrine\orm\mapping; /** * @entity * @table(name="like") */ class 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; } }
thank you.
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 */ 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 { /** * @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 likes or others because mysql keyword like
Comments
Post a Comment