symfony - __toString return name from another entity -
let have 2 tables in database : lesson , group. group can have 1 lesson per year.
then have 2 entities, lesson entity :
<?php namespace sifo\adminbundle\entity; use doctrine\orm\mapping orm; class lesson { /** * @var integer */ private $id; /** * @var string */ private $name; /** * set id * * @param integer $id * @return dftgrupmapel */ public function setid($id) { $this->id = $id; return $this; } /** * id * * @return integer */ public function getid() { return $this->id; }
and group entity :
<?php namespace sifo\adminbundle\entity; use doctrine\orm\mapping orm; class group { /** * @var integer */ private $id; /** * @var \sifo\adminbundle\entity\mstlesson */ private $idlesson; /** * @var integer */ private $year; /** * set id * * @param integer $id * @return dftgrup */ public function setid($id) { $this->id = $id; return $this; } /** * id * * @return integer */ public function getid() { return $this->id; } /** * set idlesson * * @param \sifo\adminbundle\entity\mstlesson $idlesson * @return dftgrup */ public function setidlesson(\sifo\adminbundle\entity\mstlesson $idlesson = null) { $this->idlesson = $idlesson; return $this; } /** * idgrup * * @return \sifo\adminbundle\entity\mstgrup */ public function getidlesson() { return $this->idlesson; } /** * set year * * @param integer $year * @return dftgrup */ public function setyear($year) { $this->year = $year; return $this; } /** * year * * @return integer */ public function getyear() { return $this->year; }
the problem is, in group entity don't have available string show __tostring()
. fell not making new name
field in group entity , save lesson name
there.
lesson.id
, group.idlesson
foreign key.
how can name
field in lesson entity show in __tostring()
group without create new field?
thank before.
this orm. group
class shouldn't care it's associated lesson
's id is... should have reference lesson
object itself:
<?php namespace sifo\adminbundle\entity; use doctrine\orm\mapping orm; class group { private $id; // integer private $lesson; // lesson object private $year; // integer,
once $lesson
lesson
object, can refer in group::__tostring()
:
private function __tostring() { return $this->lesson->getname(); }
here's documentation on association mapping, should helpful in mapping relationship between group
, lesson
:
http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html
Comments
Post a Comment