public function testOrphanRemoval() { $user = new CmsUser(); $user->status = 'dev'; $user->username = '******'; $user->name = 'Roman B.'; $address = new CmsAddress(); $address->country = 'de'; $address->zip = 1234; $address->city = 'Berlin'; $user->setAddress($address); $this->_em->persist($user); $this->_em->flush(); $userId = $user->getId(); $this->_em->clear(); $userProxy = $this->_em->getReference('Doctrine\\Tests\\Models\\CMS\\CmsUser', $userId); $this->_em->remove($userProxy); $this->_em->flush(); $this->_em->clear(); $query = $this->_em->createQuery('SELECT u FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u'); $result = $query->getResult(); $this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager'); $query = $this->_em->createQuery('SELECT a FROM Doctrine\\Tests\\Models\\CMS\\CmsAddress a'); $result = $query->getResult(); $this->assertEquals(0, count($result), 'CmsAddress should be removed by orphanRemoval'); }
public function testManyToManyAddRemove() { // Set up user with 2 groups $user = new CmsUser(); $user->username = '******'; $user->status = 'dev'; $user->name = 'Roman B.'; $group1 = new CmsGroup(); $group1->name = 'Developers'; $group2 = new CmsGroup(); $group2->name = 'Humans'; $user->addGroup($group1); $user->addGroup($group2); $this->_em->persist($user); // cascades to groups $this->_em->flush(); $this->_em->clear(); $uRep = $this->_em->getRepository(get_class($user)); // Get user $user = $uRep->findOneById($user->getId()); $this->assertFalse($user->getGroups()->isInitialized()); // Check groups $this->assertEquals(2, $user->getGroups()->count()); $this->assertTrue($user->getGroups()->isInitialized()); // Remove first group unset($user->groups[0]); //$user->getGroups()->remove(0); $this->_em->flush(); $this->_em->clear(); // Reload same user $user2 = $uRep->findOneById($user->getId()); // Check groups $this->assertEquals(1, $user2->getGroups()->count()); }
public function setUser(CmsUser $user) { if ($this->user !== $user) { $this->user = $user; $user->setAddress($this); } }
public function testIssue() { $phone = new CmsPhonenumber(); $phone->phonenumber = "1234"; // puts user and phone into commit order calculator $this->_em->persist($phone); $this->_em->flush(); $address = new \Doctrine\Tests\Models\CMS\CmsAddress(); $address->city = "bonn"; $address->country = "Germany"; $address->street = "somestreet!"; $address->zip = 12345; $this->_em->persist($address); $user = new CmsUser(); $user->username = "******"; $user->name = "benjamin"; $user->status = "active"; $user->setAddress($address); // puts user and address into commit order calculator, but does not calculate user dependencies new $this->_em->persist($user); $this->_em->flush(); $this->_em->remove($user->getAddress()); $this->_em->remove($user); $this->_em->flush(); }
public function testSerializeUnserializeModifyMerge() { //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $ph1 = new CmsPhonenumber(); $ph1->phonenumber = 1234; $user->addPhonenumber($ph1); $this->_em->persist($user); $this->_em->flush(); $this->assertTrue($this->_em->contains($user)); $this->assertTrue($user->phonenumbers->isInitialized()); $serialized = serialize($user); $this->_em->clear(); $this->assertFalse($this->_em->contains($user)); unset($user); $user = unserialize($serialized); $ph2 = new CmsPhonenumber(); $ph2->phonenumber = 56789; $user->addPhonenumber($ph2); $this->assertEquals(2, count($user->getPhonenumbers())); $this->assertFalse($this->_em->contains($user)); $this->_em->persist($ph2); // Merge back in $user = $this->_em->merge($user); // merge cascaded to phonenumbers $this->_em->flush(); $this->assertTrue($this->_em->contains($user)); $this->assertEquals(2, count($user->getPhonenumbers())); $phonenumbers = $user->getPhonenumbers(); $this->assertTrue($this->_em->contains($phonenumbers[0])); $this->assertTrue($this->_em->contains($phonenumbers[1])); }
public function setUp() { $this->useModelSet('cms'); parent::setUp(); $user1 = new CmsUser(); $user1->username = "******"; $user1->name = "Benjamin"; $user1->status = "active"; $group1 = new CmsGroup(); $group1->name = "test"; $group2 = new CmsGroup(); $group2->name = "test"; $user1->addGroup($group1); $user1->addGroup($group2); $user2 = new CmsUser(); $user2->username = "******"; $user2->name = "Roman"; $user2->status = "active"; $this->_em->persist($user1); $this->_em->persist($user2); $this->_em->persist($group1); $this->_em->persist($group2); $this->_em->flush(); $this->_em->clear(); $this->user1 = $this->_em->find(get_class($user1), $user1->id); $this->user2 = $this->_em->find(get_class($user1), $user2->id); }
public function testJoinQueries() { $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $article1 = new CmsArticle(); $article1->topic = "Doctrine 2"; $article1->text = "This is an introduction to Doctrine 2."; $user->addArticle($article1); $article2 = new CmsArticle(); $article2->topic = "Symfony 2"; $article2->text = "This is an introduction to Symfony 2."; $user->addArticle($article2); $this->_em->persist($user); $this->_em->persist($article1); $this->_em->persist($article2); $this->_em->flush(); $this->_em->clear(); $query = $this->_em->createQuery("select u, a from Doctrine\\Tests\\Models\\CMS\\CmsUser u join u.articles a"); $users = $query->getResult(); $this->assertEquals(1, count($users)); $this->assertTrue($users[0] instanceof CmsUser); $this->assertEquals(2, count($users[0]->articles)); $this->assertEquals('Doctrine 2', $users[0]->articles[0]->topic); $this->assertEquals('Symfony 2', $users[0]->articles[1]->topic); }
/** * @return CmsUser */ private function createNewValidUser() { $user = new CmsUser(); $user->username = '******'; $user->name = 'Francisco Facioni'; $group = new CmsGroup(); $group->name = "users"; $user->addGroup($group); return $user; }
public function testSerializeUnserializeModifyMerge() { //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); $user = new CmsUser; $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $ph1 = new CmsPhonenumber; $ph1->phonenumber = "1234"; $user->addPhonenumber($ph1); $this->_em->persist($user); $this->_em->flush(); $this->assertTrue($this->_em->contains($user)); $this->assertTrue($user->phonenumbers->isInitialized()); $serialized = serialize($user); $this->_em->clear(); $this->assertFalse($this->_em->contains($user)); unset($user); $user = unserialize($serialized); $this->assertEquals(1, count($user->getPhonenumbers()), "Pre-Condition: 1 Phonenumber"); $ph2 = new CmsPhonenumber; $ph2->phonenumber = "56789"; $user->addPhonenumber($ph2); $oldPhonenumbers = $user->getPhonenumbers(); $this->assertEquals(2, count($oldPhonenumbers), "Pre-Condition: 2 Phonenumbers"); $this->assertFalse($this->_em->contains($user)); $this->_em->persist($ph2); // Merge back in $user = $this->_em->merge($user); // merge cascaded to phonenumbers $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user->phonenumbers[0]->user); $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user->phonenumbers[1]->user); $im = $this->_em->getUnitOfWork()->getIdentityMap(); $this->_em->flush(); $this->assertTrue($this->_em->contains($user), "Failed to assert that merged user is contained inside EntityManager persistence context."); $phonenumbers = $user->getPhonenumbers(); $this->assertNotSame($oldPhonenumbers, $phonenumbers, "Merge should replace the Detached Collection with a new PersistentCollection."); $this->assertEquals(2, count($phonenumbers), "Failed to assert that two phonenumbers are contained in the merged users phonenumber collection."); $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsPhonenumber', $phonenumbers[1]); $this->assertTrue($this->_em->contains($phonenumbers[1]), "Failed to assert that second phonenumber in collection is contained inside EntityManager persistence context."); $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsPhonenumber', $phonenumbers[0]); $this->assertTrue($this->_em->getUnitOfWork()->isInIdentityMap($phonenumbers[0])); $this->assertTrue($this->_em->contains($phonenumbers[0]), "Failed to assert that first phonenumber in collection is contained inside EntityManager persistence context."); }
public function buildUser($name, $username, $status, $address) { $user = new CmsUser(); $user->name = $name; $user->username = $username; $user->status = $status; $user->setAddress($address); $this->_em->persist($user); $this->_em->flush(); return $user; }
/** * Verifying that proxies can be used without problems as query parameters */ public function testFindWithProxyName() { $result = $this->_em->find('Doctrine\\Tests\\Proxies\\__CG__\\Doctrine\\Tests\\Models\\CMS\\CmsUser', $this->user->getId()); $this->assertSame($this->user->getId(), $result->getId()); $this->_em->clear(); $result = $this->_em->getReference('Doctrine\\Tests\\Proxies\\__CG__\\Doctrine\\Tests\\Models\\CMS\\CmsUser', $this->user->getId()); $this->assertSame($this->user->getId(), $result->getId()); $this->_em->clear(); $result = $this->_em->getRepository('Doctrine\\Tests\\Proxies\\__CG__\\Doctrine\\Tests\\Models\\CMS\\CmsUser')->findOneBy(array('username' => $this->user->username)); $this->assertSame($this->user->getId(), $result->getId()); $this->_em->clear(); $result = $this->_em->createQuery('SELECT u FROM Doctrine\\Tests\\Proxies\\__CG__\\Doctrine\\Tests\\Models\\CMS\\CmsUser u WHERE u.id = ?1')->setParameter(1, $this->user->getId())->getSingleResult(); $this->assertSame($this->user->getId(), $result->getId()); $this->_em->clear(); }
protected function setUp() { $this->useModelSet('cms'); parent::setUp(); $user = new CmsUser(); $user->status = 'dev'; $user->username = '******'; $user->name = 'Roman B.'; $phone = new CmsPhonenumber(); $phone->phonenumber = '123456'; $user->addPhonenumber($phone); $this->_em->persist($user); $this->_em->flush(); $this->userId = $user->getId(); $this->_em->clear(); }
public function testGivenOrphanRemovalOneToOne_WhenReplacing_ThenNoUniqueConstraintError() { $user = new CmsUser(); $user->name = "Benjamin"; $user->username = "******"; $user->status = "something"; $user->setEmail($email = new CmsEmail()); $email->setEmail("*****@*****.**"); $this->_em->persist($user); $this->_em->flush(); $this->assertTrue($this->_em->contains($email)); $user->setEmail($newEmail = new CmsEmail()); $newEmail->setEmail("*****@*****.**"); $this->_em->flush(); $this->assertFalse($this->_em->contains($email)); }
protected function createAndPersistUser() { $user = new CmsUser(); $user->name = 'Luka'; $user->username = '******'; $user->status = 'developer'; foreach (array(1111, 2222, 3333, 4444) as $number) { $phone = new CmsPhonenumber(); $phone->phonenumber = $number; $user->addPhonenumber($phone); } foreach (array('Moshers', 'Headbangers') as $groupName) { $group = new CmsGroup(); $group->setName($groupName); $user->addGroup($group); } $this->_em->persist($user); return $user; }
public function testIssue() { $em = $this->_em; $uow = $em->getUnitOfWork(); $originalArticle = new CmsArticle(); $originalUser = new CmsUser(); $originalArticle->topic = 'Unit Test'; $originalArticle->text = 'How to write a test'; $originalUser->name = 'Doctrine Bot'; $originalUser->username = '******'; $originalUser->status = 'active'; $originalUser->addArticle($originalArticle); $em->persist($originalUser); $em->persist($originalArticle); $em->flush(); $em->clear(); $article = $em->find('Doctrine\\Tests\\Models\\CMS\\CmsArticle', $originalArticle->id); $user = new CmsUser(); $user->name = 'Doctrine Bot 2.0'; $user->username = '******'; $user->status = 'new'; $article->setAuthor($user); $this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($originalArticle)); $this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($originalUser)); $this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($article)); $this->assertEquals(UnitOfWork::STATE_NEW, $uow->getEntityState($user)); $em->detach($user); $em->detach($article); $userMerged = $em->merge($user); $articleMerged = $em->merge($article); $this->assertEquals(UnitOfWork::STATE_NEW, $uow->getEntityState($user)); $this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($article)); $this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($userMerged)); $this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($articleMerged)); $this->assertNotSame($user, $userMerged); $this->assertNotSame($article, $articleMerged); $this->assertNotSame($userMerged, $articleMerged->user); $this->assertSame($user, $articleMerged->user); }
public function testOrphanRemovalWhenUnlink() { $user = new CmsUser(); $user->status = 'dev'; $user->username = '******'; $user->name = 'Bejamin Eberlei'; $email = new CmsEmail(); $email->email = '*****@*****.**'; $user->setEmail($email); $this->_em->persist($user); $this->_em->flush(); $userId = $user->getId(); $this->_em->clear(); $user = $this->_em->find('Doctrine\\Tests\\Models\\CMS\\CmsUser', $userId); $user->setEmail(null); $this->_em->persist($user); $this->_em->flush(); $this->_em->clear(); $query = $this->_em->createQuery('SELECT e FROM Doctrine\\Tests\\Models\\CMS\\CmsEmail e'); $result = $query->getResult(); $this->assertEquals(0, count($result), 'CmsEmail should be removed by orphanRemoval'); }
/** * @group DDC-767 */ public function testCollectionChangesInsideTransaction() { $user = new CmsUser(); $user->name = "beberlei"; $user->status = "active"; $user->username = "******"; $group1 = new CmsGroup(); $group1->name = "foo"; $group2 = new CmsGroup(); $group2->name = "bar"; $group3 = new CmsGroup(); $group3->name = "baz"; $user->addGroup($group1); $user->addGroup($group2); $this->_em->persist($user); $this->_em->persist($group1); $this->_em->persist($group2); $this->_em->persist($group3); $this->_em->flush(); $this->_em->clear(); /* @var $pUser CmsUser */ $pUser = $this->_em->find(get_class($user), $user->id); $this->assertNotNull($pUser, "User not retrieved from database."); $groups = array($group2->id, $group3->id); try { $this->_em->beginTransaction(); $pUser->groups->clear(); $this->_em->flush(); // Add new foreach ($groups as $groupId) { $pUser->addGroup($this->_em->find(get_class($group1), $groupId)); } $this->_em->flush(); $this->_em->commit(); } catch (\Exception $e) { $this->_em->rollback(); } }
public function testJoinedOneToOneNativeQuery() { $user = new CmsUser(); $user->name = 'Roman'; $user->username = '******'; $user->status = 'dev'; $addr = new CmsAddress(); $addr->country = 'germany'; $addr->zip = 10827; $addr->city = 'Berlin'; $user->setAddress($addr); $this->_em->persist($user); $this->_em->flush(); $this->_em->clear(); $rsm = new ResultSetMapping(); $rsm->addEntityResult('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'u'); $rsm->addFieldResult('u', $this->platform->getSQLResultCasing('id'), 'id'); $rsm->addFieldResult('u', $this->platform->getSQLResultCasing('name'), 'name'); $rsm->addFieldResult('u', $this->platform->getSQLResultCasing('status'), 'status'); $rsm->addJoinedEntityResult('Doctrine\\Tests\\Models\\CMS\\CmsAddress', 'a', 'u', 'address'); $rsm->addFieldResult('a', $this->platform->getSQLResultCasing('a_id'), 'id'); $rsm->addFieldResult('a', $this->platform->getSQLResultCasing('country'), 'country'); $rsm->addFieldResult('a', $this->platform->getSQLResultCasing('zip'), 'zip'); $rsm->addFieldResult('a', $this->platform->getSQLResultCasing('city'), 'city'); $query = $this->_em->createNativeQuery('SELECT u.id, u.name, u.status, a.id AS a_id, a.country, a.zip, a.city FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id WHERE u.username = ?', $rsm); $query->setParameter(1, 'romanb'); $users = $query->getResult(); $this->assertEquals(1, count($users)); $this->assertTrue($users[0] instanceof CmsUser); $this->assertEquals('Roman', $users[0]->name); $this->assertTrue($users[0]->getPhonenumbers() instanceof \Doctrine\ORM\PersistentCollection); $this->assertFalse($users[0]->getPhonenumbers()->isInitialized()); $this->assertTrue($users[0]->getAddress() instanceof CmsAddress); $this->assertTrue($users[0]->getAddress()->getUser() == $users[0]); $this->assertEquals('germany', $users[0]->getAddress()->getCountry()); $this->assertEquals(10827, $users[0]->getAddress()->getZipCode()); $this->assertEquals('Berlin', $users[0]->getAddress()->getCity()); }
public function benchPersistMany() { $parent1 = new ParentTestObj(); $parent1->nodename = "root1"; $parent1->name = "root1"; $parent1->setParentDocument($this->root); $parent2 = new ParentTestObj(); $parent2->name = "/root2"; $parent2->nodename = "root2"; $parent2->setParentDocument($this->root); $child = new ParentNoNodeNameTestObj(); $child->setParentDocument($parent1); $child->name = "child"; $c1 = new Comment(); $c1->name = 'c1'; $c1->parent = $this->root; $c1->setText('deutsch'); $group1 = new \Doctrine\Tests\Models\CMS\CmsGroup(); $group1->name = "Test!"; $group1->id = '/functional/group1'; $group2 = new \Doctrine\Tests\Models\CMS\CmsGroup(); $group2->name = "Test!"; $group2->id = '/functional/group2'; $user = new \Doctrine\Tests\Models\CMS\CmsUser(); $user->username = "******"; $user->name = "Benjamin"; $user->addGroup($group1); $user->addGroup($group2); $this->documentManager->persist($parent1); $this->documentManager->persist($parent2); $this->documentManager->persist($child); $this->documentManager->persist($c1); $this->documentManager->persist($user); $this->documentManager->persist($group1); $this->documentManager->persist($group2); $this->documentManager->flush(); }
public function testOrphanRemoval() { $user = new CmsUser(); $user->status = 'dev'; $user->username = '******'; $user->name = 'Roman B.'; $phone = new CmsPhonenumber(); $phone->phonenumber = '123456'; $user->addPhonenumber($phone); $this->_em->persist($user); $this->_em->flush(); $userId = $user->getId(); $this->_em->clear(); $userProxy = $this->_em->getReference('Doctrine\\Tests\\Models\\CMS\\CmsUser', $userId); $this->_em->remove($userProxy); $this->_em->flush(); $this->_em->clear(); $query = $this->_em->createQuery('SELECT u FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u'); $result = $query->getResult(); $this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager'); $query = $this->_em->createQuery('SELECT p FROM Doctrine\\Tests\\Models\\CMS\\CmsPhonenumber p'); $result = $query->getResult(); $this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval'); }
private function loadFixtures() { $u1 = new CmsUser(); $u2 = new CmsUser(); $u3 = new CmsUser(); $u1->setEmail(new CmsEmail()); $u1->setAddress(new CmsAddress()); $u1->addPhonenumber(new CmsPhonenumber()); $u2->setEmail(new CmsEmail()); $u2->setAddress(new CmsAddress()); $u2->addPhonenumber(new CmsPhonenumber()); $u2->addPhonenumber(new CmsPhonenumber()); $u3->setEmail(new CmsEmail()); $u3->setAddress(new CmsAddress()); $u3->addPhonenumber(new CmsPhonenumber()); $u3->addPhonenumber(new CmsPhonenumber()); $u3->addPhonenumber(new CmsPhonenumber()); $u1->name = 'Test 1'; $u1->username = '******'; $u1->status = 'developer'; $u1->email->email = '*****@*****.**'; $u1->address->zip = '111111111'; $u1->address->city = 'Some City 1'; $u1->address->country = 'Some Country 2'; $u1->phonenumbers[0]->phonenumber = "(11) 1111-1111"; $u2->name = 'Test 2'; $u2->username = '******'; $u2->status = 'developer'; $u2->email->email = '*****@*****.**'; $u2->address->zip = '222222222'; $u2->address->city = 'Some City 2'; $u2->address->country = 'Some Country 2'; $u2->phonenumbers[0]->phonenumber = "(22) 1111-1111"; $u2->phonenumbers[1]->phonenumber = "(22) 2222-2222"; $u3->name = 'Test 3'; $u3->username = '******'; $u3->status = 'developer'; $u3->email->email = '*****@*****.**'; $u3->address->zip = '33333333'; $u3->address->city = 'Some City 3'; $u3->address->country = 'Some Country 3'; $u3->phonenumbers[0]->phonenumber = "(33) 1111-1111"; $u3->phonenumbers[1]->phonenumber = "(33) 2222-2222"; $u3->phonenumbers[2]->phonenumber = "(33) 3333-3333"; $this->_em->persist($u1); $this->_em->persist($u2); $this->_em->persist($u3); $this->_em->flush(); $this->_em->clear(); $this->fixtures = array($u1, $u2, $u3); }
public function testRefreshCollection() { $user = new CmsUser(); $user->id = '/functional/Guilherme'; $user->username = '******'; // Add a group $group1 = new CmsGroup(); $group1->name = "12345"; $group1->id = '/functional/group1'; $user->addGroup($group1); // Add a group $group2 = new CmsGroup(); $group2->name = "54321"; $group2->id = '/functional/group2'; $this->dm->persist($user); $this->dm->persist($group1); $this->dm->persist($group2); $this->dm->flush(); $user->addGroup($group2); $this->assertCount(2, $user->groups); $user->groups->refresh(); $this->assertCount(1, $user->groups); }
private function loadFixtureData() { $user = new CmsUser(); $user->name = 'Roman'; $user->username = '******'; $user->status = 'developer'; $address = new CmsAddress(); $address->country = 'Germany'; $address->city = 'Berlin'; $address->zip = '12345'; $user->address = $address; // inverse side $address->user = $user; // owning side! $group = new CmsGroup(); $group->name = 'foo_group'; $user->addGroup($group); $article1 = new CmsArticle(); $article1->topic = "Test1"; $article1->text = "Test"; $article1->setAuthor($user); $article2 = new CmsArticle(); $article2->topic = "Test2"; $article2->text = "Test"; $article2->setAuthor($user); $this->_em->persist($article1); $this->_em->persist($article2); $this->_em->persist($user); $user2 = new CmsUser(); $user2->name = 'Guilherme'; $user2->username = '******'; $user2->status = 'developer'; $address2 = new CmsAddress(); $address2->country = 'France'; $address2->city = 'Paris'; $address2->zip = '12345'; $user->address = $address2; // inverse side $address2->user = $user2; // owning side! $user2->addGroup($group); $group2 = new CmsGroup(); $group2->name = 'bar_group'; $user2->addGroup($group2); $this->_em->persist($user2); $this->_em->flush(); $this->_em->clear(); $this->userId = $user->getId(); $this->userId2 = $user2->getId(); $this->articleId = $article1->id; $this->articleId2 = $article2->id; $this->groupId = $group->id; $this->groupId2 = $group2->id; }
/** * @expectedException Doctrine\ORM\NonUniqueResultException */ public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult() { $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $article1 = new CmsArticle(); $article1->topic = "Doctrine 2"; $article1->text = "This is an introduction to Doctrine 2."; $user->addArticle($article1); $article2 = new CmsArticle(); $article2->topic = "Symfony 2"; $article2->text = "This is an introduction to Symfony 2."; $user->addArticle($article2); $this->_em->persist($user); $this->_em->persist($article1); $this->_em->persist($article2); $this->_em->flush(); $this->_em->clear(); $this->_em->createQuery("select a from Doctrine\\Tests\\Models\\CMS\\CmsArticle a")->getSingleScalarResult(); }
public function loadAssociatedFixture() { $address = new CmsAddress(); $address->city = "Berlin"; $address->country = "Germany"; $address->street = "Foostreet"; $address->zip = "12345"; $user = new CmsUser(); $user->name = 'Roman'; $user->username = '******'; $user->status = 'freak'; $user->setAddress($address); $this->_em->persist($user); $this->_em->persist($address); $this->_em->flush(); $this->_em->clear(); return array($user->id, $address->id); }
/** * @param int $groupCount * @return CmsUser */ public function addCmsUserGblancoWithGroups($groupCount = 1) { $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; for ($i = 0; $i < $groupCount; ++$i) { $group = new CmsGroup(); $group->name = 'Developers_' . $i; $user->addGroup($group); } $this->_em->persist($user); $this->_em->flush(); $this->assertNotNull($user->getId(), "User 'gblanco' should have an ID assigned after the persist()/flush() operation."); return $user; }
/** * @group DDC-1663 * DQL : SELECT u, a, COUNT(p) AS numphones FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.address a JOIN u.phonenumbers p */ public function testMultipleEntityResults() { $user = new CmsUser(); $user->name = 'Fabio B. Silva'; $user->username = '******'; $user->status = 'dev'; $addr = new CmsAddress(); $addr->country = 'Brazil'; $addr->zip = 10827; $addr->city = 'São Paulo'; $phone = new CmsPhonenumber(); $phone->phonenumber = 424242; $user->setAddress($addr); $user->addPhonenumber($phone); $this->_em->clear(); $this->_em->persist($user); $this->_em->flush(); $this->_em->clear(); $repository = $this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsUser'); $query = $repository->createNativeNamedQuery('fetchMultipleJoinsEntityResults'); $result = $query->getResult(); $this->assertEquals(1, count($result)); $this->assertTrue(is_array($result[0])); $this->assertInstanceOf('Doctrine\\Tests\\Models\\CMS\\CmsUser', $result[0][0]); $this->assertEquals('Fabio B. Silva', $result[0][0]->name); $this->assertInstanceOf('Doctrine\\Tests\\Models\\CMS\\CmsAddress', $result[0][0]->getAddress()); $this->assertTrue($result[0][0]->getAddress()->getUser() == $result[0][0]); $this->assertEquals('Brazil', $result[0][0]->getAddress()->getCountry()); $this->assertEquals(10827, $result[0][0]->getAddress()->getZipCode()); $this->assertEquals(1, $result[0]['numphones']); }
/** * @group DDC-634 */ public function testOneToOneMergeSetNull() { //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); $user = new CmsUser(); $user->username = "******"; $user->name = "Benjamin E."; $user->status = 'active'; $ph = new CmsPhonenumber(); $ph->phonenumber = "12345"; $user->addPhonenumber($ph); $this->_em->persist($user); $this->_em->persist($ph); $this->_em->flush(); $this->_em->clear(); $ph->user = null; $managedPh = $this->_em->merge($ph); $this->_em->flush(); $this->_em->clear(); $this->assertNull($this->_em->find(get_class($ph), $ph->phonenumber)->getUser()); }
/** * @group ref */ public function testQueryEntityByReference() { $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $address = new CmsAddress(); $address->country = 'Germany'; $address->city = 'Berlin'; $address->zip = '12345'; $user->setAddress($address); $this->_em->transactional(function ($em) use($user) { $em->persist($user); }); $this->_em->clear(); //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); $userRef = $this->_em->getReference('Doctrine\\Tests\\Models\\CMS\\CmsUser', $user->getId()); $address2 = $this->_em->createQuery('select a from Doctrine\\Tests\\Models\\CMS\\CmsAddress a where a.user = :user')->setParameter('user', $userRef)->getSingleResult(); $this->assertTrue($address2->getUser() instanceof \Doctrine\ORM\Proxy\Proxy); $this->assertTrue($userRef === $address2->getUser()); $this->assertFalse($userRef->__isInitialized__); $this->assertEquals('Germany', $address2->country); $this->assertEquals('Berlin', $address2->city); $this->assertEquals('12345', $address2->zip); }
/** * Test that changing associations on detached entities and then cascade merging them causes the * database to be updated with the new associations. */ public function testManyToManyMergeAssociationRemoves() { $this->setCascadeMergeFor('Doctrine\\Tests\\Models\\CMS\\CmsUser'); $this->setCascadeMergeFor('Doctrine\\Tests\\Models\\CMS\\CmsGroup'); $cmsUser = new CmsUser(); $cmsUser->username = "******"; $cmsUser->name = "Dave Keen"; $cmsUser->status = "testing"; $group1 = new CmsGroup(); $group1->name = "Group 1"; $group2 = new CmsGroup(); $group2->name = "Group 2"; $cmsUser->addGroup($group1); $cmsUser->addGroup($group2); $this->_em->persist($cmsUser); $this->_em->persist($group1); $this->_em->persist($group2); $this->_em->flush(); $cmsUserId = $cmsUser->id; $group1Id = $group1->id; $group2Id = $group2->id; $this->_em->clear(); // Now create detached versions of the entities with NO associations. $cmsUser = new CmsUser(); $cmsUser->id = $cmsUserId; $cmsUser->username = "******"; $cmsUser->name = "Dave Keen"; $cmsUser->status = "testing"; $cmsUser->groups = new ArrayCollection(); $group1 = new CmsGroup(); $group1->id = $group1Id; $group1->name = "Group 1"; $group1->users = new ArrayCollection(); $group2 = new CmsGroup(); $group2->id = $group2Id; $group2->name = "Group 2"; $group2->users = new ArrayCollection(); // Cascade merge of cmsUser followed by a flush should result in the association array collection being empty $this->_em->merge($cmsUser); $this->_em->flush(); $this->_em->clear(); $cmsUsers = $this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsUser')->findAll(); $cmsGroups = $this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsGroup')->findAll(); // Check the entities are in the database $this->assertEquals(1, sizeof($cmsUsers)); $this->assertEquals(2, sizeof($cmsGroups)); // Check the associations between the entities are now in the database $this->assertEquals(0, sizeof($cmsUsers[0]->groups)); $this->assertEquals(0, sizeof($cmsGroups[0]->users)); $this->assertEquals(0, sizeof($cmsGroups[1]->users)); }