2009-01-07 20:46:02 +03:00
|
|
|
<?php
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Export\ClassExporter;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
2009-02-02 14:55:50 +03:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress;
|
2009-02-06 20:16:39 +03:00
|
|
|
use Doctrine\Tests\Models\CMS\CmsGroup;
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-01-24 19:56:44 +03:00
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
2009-01-07 20:46:02 +03:00
|
|
|
|
2009-02-18 10:59:11 +03:00
|
|
|
class BasicCRUDTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
public function testBasicUnitsOfWorkWithOneToManyAssociation()
|
|
|
|
{
|
2009-01-12 16:34:41 +03:00
|
|
|
$em = $this->_em;
|
2009-01-07 20:46:02 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
$exporter = new ClassExporter($this->_em);
|
2009-01-08 14:23:24 +03:00
|
|
|
$exporter->exportClasses(array(
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'),
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'),
|
2009-02-06 20:16:39 +03:00
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'),
|
|
|
|
$this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsGroup')
|
2009-01-09 19:25:06 +03:00
|
|
|
));
|
2009-01-07 20:46:02 +03:00
|
|
|
|
|
|
|
// Create
|
|
|
|
$user = new CmsUser;
|
2009-02-02 14:55:50 +03:00
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-01-07 20:46:02 +03:00
|
|
|
$em->save($user);
|
|
|
|
$this->assertTrue(is_numeric($user->id));
|
|
|
|
$this->assertTrue($em->contains($user));
|
|
|
|
|
|
|
|
// Read
|
2009-01-22 22:38:10 +03:00
|
|
|
$user2 = $em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
|
2009-01-09 19:25:06 +03:00
|
|
|
$this->assertTrue($user === $user2);
|
2009-01-08 14:23:24 +03:00
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
// Add a phonenumber
|
2009-01-08 14:23:24 +03:00
|
|
|
$ph = new CmsPhonenumber;
|
|
|
|
$ph->phonenumber = "12345";
|
2009-01-09 19:25:06 +03:00
|
|
|
$user->addPhonenumber($ph);
|
|
|
|
$em->flush();
|
|
|
|
$this->assertTrue($em->contains($ph));
|
|
|
|
$this->assertTrue($em->contains($user));
|
2009-02-06 20:16:39 +03:00
|
|
|
//$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
|
2009-01-08 14:23:24 +03:00
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
// Update name
|
2009-01-09 19:25:06 +03:00
|
|
|
$user->name = 'guilherme';
|
|
|
|
$em->flush();
|
|
|
|
$this->assertEquals('guilherme', $user->name);
|
|
|
|
|
2009-01-29 20:00:44 +03:00
|
|
|
// Add another phonenumber
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "6789";
|
|
|
|
$user->addPhonenumber($ph2);
|
|
|
|
$em->flush();
|
|
|
|
$this->assertTrue($em->contains($ph2));
|
|
|
|
|
2009-01-09 19:25:06 +03:00
|
|
|
// Delete
|
|
|
|
$em->delete($user);
|
|
|
|
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($user));
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($ph));
|
|
|
|
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($ph2));
|
2009-01-09 19:25:06 +03:00
|
|
|
$em->flush();
|
|
|
|
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($user));
|
2009-01-29 20:00:44 +03:00
|
|
|
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($ph));
|
|
|
|
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($ph2));
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
2009-01-08 14:23:24 +03:00
|
|
|
|
2009-02-18 10:59:11 +03:00
|
|
|
public function testOneToManyAssociationModification()
|
|
|
|
{
|
2009-02-02 14:55:50 +03:00
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
$ph1 = new CmsPhonenumber;
|
|
|
|
$ph1->phonenumber = "0301234";
|
|
|
|
$ph2 = new CmsPhonenumber;
|
|
|
|
$ph2->phonenumber = "987654321";
|
|
|
|
|
|
|
|
$user->addPhonenumber($ph1);
|
|
|
|
$user->addPhonenumber($ph2);
|
|
|
|
|
|
|
|
$this->_em->save($user);
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
//$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
// Remove the first element from the collection
|
|
|
|
unset($user->phonenumbers[0]);
|
|
|
|
$ph1->user = null; // owning side!
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($user->phonenumbers));
|
|
|
|
$this->assertNull($ph1->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicOneToOne()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Roman';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->status = 'developer';
|
2009-01-12 16:34:41 +03:00
|
|
|
|
2009-02-02 14:55:50 +03:00
|
|
|
$address = new CmsAddress;
|
|
|
|
$address->country = 'Germany';
|
|
|
|
$address->city = 'Berlin';
|
|
|
|
$address->zip = '12345';
|
|
|
|
|
|
|
|
$user->address = $address; // inverse side
|
|
|
|
$address->user = $user; // owning side!
|
|
|
|
|
|
|
|
$this->_em->save($user);
|
2009-01-12 16:34:41 +03:00
|
|
|
$this->_em->flush();
|
2009-02-02 14:55:50 +03:00
|
|
|
|
|
|
|
// Check that the foreign key has been set
|
|
|
|
$userId = $this->_em->getConnection()->execute("SELECT user_id FROM cms_addresses WHERE id=?",
|
|
|
|
array($address->id))->fetchColumn();
|
|
|
|
$this->assertTrue(is_numeric($userId));
|
|
|
|
}
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
public function testBasicManyToMany()
|
|
|
|
{
|
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
$group = new CmsGroup;
|
|
|
|
$group->name = 'Developers';
|
|
|
|
|
|
|
|
$user->groups[] = $group;
|
|
|
|
$group->users[] = $user;
|
|
|
|
|
|
|
|
$this->_em->save($user);
|
|
|
|
$this->_em->save($group);
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
unset($group->users[0]); // inverse side
|
|
|
|
unset($user->groups[0]); // owning side!
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
// Check that the link in the association table has been deleted
|
|
|
|
$count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
|
|
|
|
array())->fetchColumn();
|
|
|
|
$this->assertEquals(0, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManyToManyCollectionClearing()
|
|
|
|
{
|
2009-02-07 20:02:13 +03:00
|
|
|
echo PHP_EOL . "MANY-MANY" . PHP_EOL;
|
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
$user = new CmsUser;
|
|
|
|
$user->name = 'Guilherme';
|
|
|
|
$user->username = 'gblanco';
|
|
|
|
$user->status = 'developer';
|
|
|
|
|
|
|
|
for ($i=0; $i<10; ++$i) {
|
|
|
|
$group = new CmsGroup;
|
|
|
|
$group->name = 'Developers_' . $i;
|
|
|
|
$user->groups[] = $group;
|
|
|
|
$group->users[] = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_em->save($user); // Saves the user, cause of post-insert ID
|
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
// Check that there are indeed 10 links in the association table
|
|
|
|
$count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
|
|
|
|
array())->fetchColumn();
|
|
|
|
$this->assertEquals(10, $count);
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
//$user->groups->clear();
|
|
|
|
unset($user->groups);
|
|
|
|
|
2009-02-07 20:02:13 +03:00
|
|
|
echo PHP_EOL . "FINAL FLUSH" . PHP_EOL;
|
2009-02-06 20:16:39 +03:00
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
// Check that the links in the association table have been deleted
|
|
|
|
$count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
|
|
|
|
array())->fetchColumn();
|
|
|
|
$this->assertEquals(0, $count);
|
|
|
|
}
|
2009-02-18 10:59:11 +03:00
|
|
|
}
|