public function testRefreshOneToOne() { $user = new CmsUser(); $user->name = "beberlei"; $user->status = "active"; $user->username = "******"; $address = new CmsAddress(); $address->city = "Bonn"; $address->country = "Germany"; $address->street = "A street"; $address->zip = 12345; $address->setUser($user); $this->_em->persist($user); $this->_em->persist($address); $this->_em->flush(); $this->_em->refresh($address); $this->assertSame($user, $address->user); $this->assertSame($user->address, $address); }
public function testSingleValuedAssociationIdentityMapBehaviorWithRefreshQuery() { $address = new CmsAddress(); $address->country = 'de'; $address->zip = '12345'; $address->city = 'Berlin'; $user1 = new CmsUser(); $user1->status = 'dev'; $user1->username = '******'; $user1->name = 'Roman B.'; $user2 = new CmsUser(); $user2->status = 'dev'; $user2->username = '******'; $user2->name = 'Guilherme Blanco'; $address->setUser($user1); $this->_em->persist($address); $this->_em->persist($user1); $this->_em->persist($user2); $this->_em->flush(); $this->assertSame($user1, $address->user); //external update to CmsAddress $this->_em->getConnection()->executeUpdate('update cms_addresses set user_id = ?', array($user2->getId())); //select $q = $this->_em->createQuery('select a, u from Doctrine\\Tests\\Models\\CMS\\CmsAddress a join a.user u'); $address2 = $q->getSingleResult(); $this->assertSame($address, $address2); // Should still be $user1 $this->assertSame($user1, $address2->user); $this->assertTrue($user2->address === null); // But we want to have this external change! // Solution 2: Alternatively, a refresh query should work $q = $this->_em->createQuery('select a, u from Doctrine\\Tests\\Models\\CMS\\CmsAddress a join a.user u'); $q->setHint(Query::HINT_REFRESH, true); $address3 = $q->getSingleResult(); $this->assertSame($address, $address3); // should still be the same, always from identity map // Now the association should be "correct", referencing $user2 $this->assertSame($user2, $address2->user); $this->assertSame($user2->address, $address2); // check back reference also // Attention! refreshes can result in broken bidirectional associations! this is currently expected! // $user1 still points to $address2! $this->assertSame($user1->address, $address2); }
public function setAddress(CmsAddress $address) { $this->address = $address; $address->setUser($this); }
public function testUninitializedLazyAssociationsAreIgnoredOnMerge() { $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $address = new CmsAddress(); $address->city = 'Berlin'; $address->country = 'Germany'; $address->street = 'Sesamestreet'; $address->zip = 12345; $address->setUser($user); $this->_em->persist($address); $this->_em->persist($user); $this->_em->flush(); $this->_em->clear(); $address2 = $this->_em->find(get_class($address), $address->id); $this->assertTrue($address2->user instanceof \Doctrine\ORM\Proxy\Proxy); $this->assertFalse($address2->user->__isInitialized__); $detachedAddress2 = unserialize(serialize($address2)); $this->assertTrue($detachedAddress2->user instanceof \Doctrine\ORM\Proxy\Proxy); $this->assertFalse($detachedAddress2->user->__isInitialized__); $managedAddress2 = $this->_em->merge($detachedAddress2); $this->assertTrue($managedAddress2->user instanceof \Doctrine\ORM\Proxy\Proxy); $this->assertFalse($managedAddress2->user === $detachedAddress2->user); $this->assertFalse($managedAddress2->user->__isInitialized__); }