Mongo Doctrine – řešil někdo inicializaci entity přes proxy v actionXXX()?

Notice: This thread is very old.
simPod
Member | 383
+
0
-

Chci se zeptat, jestli někdo vyřešil tenhle problém s Mongo Doctrine:

class Entity extends Document {

	/** @ODM\Id */
	protected $_id;

	/** @ODM\ReferenceOne(targetDocument="Entity2", simple=true) */
	protected $entity2;
    ...

tohle je bez problému:

public function renderXxx($id) {
		$this->entity = $entity = $this-myModel->find($id);
		$this->entity2 = $entity2 = $this->entity->getEntity2()->getId();
	}

Hází Doctrine\ODM\MongoDB\DocumentNotFoundException The "Proxies\__CG__\Entity2" document with identifier "54de5341642c8769150041a7" could not be found.

public function actionXxx($id) {
		$this->entity = $entity = $this-myModel->find($id);
		$this->entity2 = $entity2 = $this->entity->getEntity2()->getId();
	}

$this->entity->getEntity2() funguje, ale vrátí to instanci entity2 s $isInitialized=false. A pravděpodobně se to před renderingem neinicializuje. Někdo někdy řešil/vyřešil?

Filip Procházka
Moderator | 4668
+
0
-

To je normální chování proxy objektů. Problém je v tom, že to na co ukazuje objekt $entity2 není v tvé databázi.

simPod
Member | 383
+
0
-

Mno, existuje nějaké řešení? Resp. poukazuješ na něco co mi uniklo? Já v dané kolekci objekt s tim ObjectId mám.

Úplně nechápu, že getEntity2() mi vrátí objekt, ale asi nějak není navázáný na db? Jsem z toho trochu mimo. Díky

bazo
Member | 620
+
0
-

skus zmazat vsetky cache.

inak toto je english forum

simPod
Member | 383
+
0
-

Damn didn't notice. I was abroad so I propably got redirected. Let's keep it in English then :)

I tried cleaning cache and regenerating proxy and hydrator files but that didn't help.

This is what I get when I var_dump($this->entity->getEntity2()):

class Proxies\__CG__\Doctrine\Documents\Entities\Entity2#178 (6) {
public $__initializer__ => class Closure#160 (3) {
public $static => array(2) {
'documentPersister' => class Doctrine\ODM\MongoDB\Persisters\DocumentPersister#166 (11) { ... }
'reflectionId' => class ReflectionProperty#165 (2) { ... } }
 public $this => class Doctrine\ODM\MongoDB\Proxy\ProxyFactory#78 (7) {
private $metadataFactory => class Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory#74 (9) { ... }
private $uow => class Doctrine\ODM\MongoDB\UnitOfWork#76 (23) { ... }
private $proxyNamespace => string(7) "Proxies"
private $proxyGenerator => class Doctrine\Common\Proxy\ProxyGenerator#79 (4) { ... }
private $autoGenerate => int(1)
private $definitions => array(1) { ... }
private $metadataFactory => class Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory#74 (9) { ... } }
public $parameter => array(1) { '$proxy' => string(10) "" } }
public $__cloner__ => class Closure#170 (3) {
public $static => array(3) {
'documentPersister' => class Doctrine\ODM\MongoDB\Persisters\DocumentPersister#166 (11) { ... }
'classMetadata' => class Doctrine\ODM\MongoDB\Mapping\ClassMetadata#155 (38) { ... }
'reflectionId' => class ReflectionProperty#165 (2) { ... } }
public $this => class Doctrine\ODM\MongoDB\Proxy\ProxyFactory#78 (7) {
private $metadataFactory => class Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory#74 (9) { ... }
private $uow => class Doctrine\ODM\MongoDB\UnitOfWork#76 (23) { ... }
private $proxyNamespace => string(7) "Proxies"
private $proxyGenerator => class Doctrine\Common\Proxy\ProxyGenerator#79 (4) { ... }
private $autoGenerate => int(1) private $definitions => array(1) { ... }
private $metadataFactory => class Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory#74 (9) { ... } }
public $parameter => array(1) { '$proxy' => string(10) "" } }
public $__isInitialized__ => bool(false)
protected $_id => NULL
protected $label => NULL
protected $id => string(24) "54de5341642c8769150041a7" }

Even $id property is there, only it is $__isInitializd__ => false. And then after calling $this->entity->getEntity2()->getId(); it throws DocumentNotFoundException but only when I call it before renderXxx() method so for excample in actionXxx() or in form submit handler. When called in renderXxx() method it works…

Last edited by simPod (2015-02-14 18:43)

Quinix
Member | 108
+
0
-

$__isInitializd__ => false is normal behavior. Doctirne will (try to) load its content from database when it's actually needed – when you call some method or touch its property.

The fact that inicialization works in render but not in action is really weird. Are you sure they are exactly same in both cases?

simPod
Member | 383
+
0
-

I thought that $__isInitialized__ => false is normal behavior though that fact is really weird as you say so.

When I dump $this->entity->getEntity2() in action and in render method, those responses are completely identical. The only difference is that initialisation works only in render :/