2011-09-06 08:58:16 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser,
|
|
|
|
Doctrine\Tests\Models\CMS\CmsAddress,
|
|
|
|
Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a bidirectional one-to-many association mapping with orphan removal.
|
|
|
|
*/
|
|
|
|
class OneToManyOrphanRemovalTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|
|
|
{
|
2011-11-18 18:44:06 +04:00
|
|
|
protected $userId;
|
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->useModelSet('cms');
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
parent::setUp();
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$user = new CmsUser;
|
|
|
|
$user->status = 'dev';
|
|
|
|
$user->username = 'romanb';
|
|
|
|
$user->name = 'Roman B.';
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$phone = new CmsPhonenumber;
|
|
|
|
$phone->phonenumber = '123456';
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$user->addPhonenumber($phone);
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$this->_em->persist($user);
|
|
|
|
$this->_em->flush();
|
2011-11-18 18:44:06 +04:00
|
|
|
|
|
|
|
$this->userId = $user->getId();
|
2011-09-06 08:58:16 +04:00
|
|
|
$this->_em->clear();
|
2011-11-18 18:44:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOrphanRemoval()
|
|
|
|
{
|
|
|
|
$userProxy = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
|
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$this->_em->remove($userProxy);
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
|
|
|
|
$result = $query->getResult();
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$this->assertEquals(0, count($result), 'CmsUser should be removed by EntityManager');
|
2011-11-18 18:44:06 +04:00
|
|
|
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-1496
|
|
|
|
*/
|
|
|
|
public function testOrphanRemovalUnitializedCollection()
|
|
|
|
{
|
|
|
|
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
|
|
|
|
|
|
|
|
$user->phonenumbers->clear();
|
|
|
|
$this->_em->flush();
|
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p');
|
|
|
|
$result = $query->getResult();
|
2011-11-18 18:44:06 +04:00
|
|
|
|
2011-09-06 08:58:16 +04:00
|
|
|
$this->assertEquals(0, count($result), 'CmsPhonenumber should be removed by orphanRemoval');
|
|
|
|
}
|
|
|
|
}
|