php - Symfony2 DiscriminatorColumn as Foreign Key and load Entity -
i have inheritancetype
of single_table
, type based on fk entity.
when goto detail page attachment entity loads product entity. when try access product entity via item attachmentcollection product null.
the controllers views using '$entity = $em->getrepository('projecttestbundle:item')->find($id);' '$entity = $em->getrepository('projecttestbundle:attachment')->find($id);'
dumps , files below.
dump of entity on attachment detail page
imageattachment {#361 ▼ #product: product {#390 ▼ +__isinitialized__: false #id: "image" #name: null …4 } -id: 1 #name: "test" #item: item {#372 ▶} }
dump of entity on item detail page via collection
imageattachment {#367 ▼ #product: null -id: 1 #name: "test" #item: item {#323 ▶} }
attachment.php
namespace project\testbundle\entity; use doctrine\orm\mapping orm; /** * attachment * * @orm\table(name="attachments") * @orm\entity * @orm\inheritancetype("single_table") * @orm\discriminatorcolumn(name="product", type="string") * @orm\discriminatormap({"image" = "project\testbundle\entity\imageattachment", "text" = "project\testbundle\entity\textattachment"}) * @orm\entity(repositoryclass="project\testbundle\entity\attachmentrepository") */ class attachment { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(type="string") */ protected $name; /** * @orm\manytoone( * targetentity="item", * inversedby="attachments" * ) * @orm\joincolumn( * name="item_id", * referencedcolumnname="id", * nullable=false * ) */ protected $item; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set name * * @param string $name * @return attachment */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } /** * set item * * @param \project\testbundle\entity\item $item * @return attachment */ public function setitem(\project\testbundle\entity\item $item) { $this->item = $item; return $this; } /** * item * * @return \project\testbundle\entity\item */ public function getitem() { return $this->item; } } ?>
imageattachment.php (textattachemnt same class simple text changes now.)
namespace project\testbundle\entity; use doctrine\orm\mapping orm; /** * attachment * * @orm\entity */ class imageattachment extends attachment { /** * @orm\manytoone(targetentity="product",cascade={"persist"},fetch="extra_lazy") * @orm\joincolumn(name="product", referencedcolumnname="product", nullable=true) */ protected $product; /** * set product * * @param \project\testbundle\entity\product $product * @return imageattachment */ public function setproduct(\project\testbundle\entity\product $product = null) { $this->product = $product; return $this; } /** * product * * @return \project\testbundle\entity\product */ public function getproduct() { return $this->product; } /** * set item * * @param \project\testbundle\entity\item $item * @return imageattachment */ public function setitem(\project\testbundle\entity\item $item) { $this->item = $item; return $this; } /** * item * * @return \project\testbundle\entity\item */ public function getitem() { return $this->item; } }
item.php
namespace project\testbundle\entity; use doctrine\orm\mapping orm; /** * item * * @orm\table(name="items") * @orm\entity * @orm\entity(repositoryclass="project\testbundle\entity\itemrepository") */ class item { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(type="string") */ protected $name; /** * @orm\onetomany( * targetentity="attachment", * mappedby="item", * cascade={"persist", "remove"}, * orphanremoval=true * ) */ protected $attachments; /** * id * * @return integer */ public function getid() { return $this->id; } /** * constructor */ public function __construct() { $this->attachments = new \doctrine\common\collections\arraycollection(); } /** * set name * * @param string $name * @return item */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } /** * add attachments * * @param \project\testbundle\entity\attachment $attachments * @return item */ public function addattachment(\project\testbundle\entity\attachment $attachments) { $this->attachments[] = $attachments; $attachments->setitem($this); return $this; } /** * remove attachments * * @param \project\testbundle\entity\attachment $attachments */ public function removeattachment(\project\testbundle\entity\attachment $attachments) { $this->attachments->removeelement($attachments); } /** * attachments * * @return \doctrine\common\collections\collection */ public function getattachments() { return $this->attachments; } }
i think problem product entity lazy loaded, in attachment entity. avoid that, should use custom method find entity, left join product, :
/* controller*/ $entity = $em->getrepository('projecttestbundle:item')->customfind($id); /* itemrepository */ $em = $this->getentitymanager(); $qb = $em->createquerybuilder(); $qb ->select('item','attachment') ->from('projettestbundle\entity\item','item') ->leftjoin('project.attachment','attachment') ; $q = $qb->getquery(); return $q->getresult();
hope helps
Comments
Post a Comment