php - Doctrine related entities return all fields values NULL -
i using symfony 3.1.10 on server php 5.6.31 (with zend opcache 7.0.6-dev), apache , mysql (ver 14.14 distrib 5.6.37).
i have entity called document related entity called worker
the relationship have worked untill today... non when retrive document worker, worker field null (except id).
so if in db there worker fields: id 4, name: john, age: 33 if retrive document worker relater , dump $document->getworker() return objet worker id: 4, name: null, age: null.
the document class
namespace appbundle\entity; use doctrine\orm\mapping orm; /** * @orm\entity * @orm\table(name="documents") * @orm\entity(repositoryclass="appbundle\repository\documentrepository") */ class document { /** * @orm\id * @orm\column(type="integer") * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="string", nullable=true) */ protected $object; /** * @orm\manytoone(targetentity="appbundle\entity\worker", cascade={"persist"}, inversedby="documents") * @orm\joincolumn(name="worker_id", referencedcolumnname="id", ondelete="set null") */ protected $worker; public function getid() { return $this->id; } public function setobject($object) { $this->object = $object; return $this; } public function getobject() { return $this->object; } public function setworker(\appbundle\entity\worker $worker = null) { $this->worker = $worker; return $this; } public function getworker() { return $this->worker; } }
the worker class
namespace appbundle\entity; use doctrine\common\collections\arraycollection; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; /** * @orm\entity * @orm\table(name="workers") * @orm\entity(repositoryclass="appbundle\repository\workerrepository") */ class worker { /** * @orm\id * @orm\column(type="integer") * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="string", nullable=true) * @assert\notblank() */ protected $firstname; /** * @orm\column(type="string", nullable=true) * @assert\notblank() */ protected $lastname; /** * @orm\column(type="string", nullable=true) * @assert\notblank() */ protected $age; /** * @orm\onetomany(targetentity="appbundle\entity\document", mappedby="worker") */ protected $documents; public function __construct() { $this->documents = new arraycollection(); } public function __tostring() { return $this->firstname . ' ' . $this->lastname; } public function getid() { return $this->id; } public function setfirstname($firstname) { $this->firstname = $firstname; return $this; } public function getfirstname() { return $this->firstname; } public function setlastname($lastname) { $this->lastname = $lastname; return $this; } public function getlastname() { return $this->lastname; } public function setage($age) { $this->age = $age; return $this; } public function getage() { return $this->age; } public function adddocument(\appbundle\entity\document $document) { $this->documents[] = $document; return $this; } public function removedocument(\appbundle\entity\document $document) { $this->documents->removeelement($document); } public function getdocuments() { return $this->documents; } }
i read here: doctrine return null values in production
i tried everything, nothing works.
this looks lazy loading problem : see, when query document since query doesn't explicitly ask worker, not loaded. reference loaded.
and here, never call worker why worker {#701 ▼ +__isinitialized__: false
first have tried calling $document->getworker()
outside of dump()
? dump won't fetch nested collection avoid overwhelm memory. should work in twig or in basic echo. echo $document->getworker()->getfirstname()
when worker, use symfony debugger see threw 2 queries, 1 document , 1 worker. should avoid this. called lazy loading. never asked worker doctrine pretty fine orm , smart enough load itself.
it not problematic when work 1 entity, think db work when fetching list of documents.
one better way use custom query in repository.
Comments
Post a Comment