1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Merge pull request #153 from asm89/shesek-patch-1

Support NULL in EntityRepository's magic findBy and findOneBy methods
This commit is contained in:
Benjamin Eberlei 2011-10-17 12:05:07 -07:00
commit 0b1f6e3539
3 changed files with 33 additions and 16 deletions

View File

@ -204,8 +204,7 @@ class EntityRepository implements ObjectRepository
);
}
if ( !isset($arguments[0])) {
// we dont even want to allow null at this point, because we cannot (yet) transform it into IS NULL.
if (empty($arguments)) {
throw ORMException::findByRequiresParameter($method.$by);
}

View File

@ -19,7 +19,7 @@ class CmsUser
*/
public $id;
/**
* @Column(type="string", length=50)
* @Column(type="string", length=50, nullable=true)
*/
public $status;
/**

View File

@ -38,18 +38,25 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$user2->status = 'dev';
$this->_em->persist($user2);
$user3 = new CmsUser;
$user3->name = 'Benjamin';
$user3->username = 'beberlei';
$user3->status = null;
$this->_em->persist($user3);
$this->_em->flush();
$user1Id = $user->getId();
unset($user);
unset($user2);
unset($user3);
$this->_em->clear();
return $user1Id;
}
public function loadAssociatedFixture()
{
$address = new CmsAddress();
@ -189,7 +196,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$users = $repos->findAll();
$this->assertEquals(2, count($users));
$this->assertEquals(3, count($users));
}
public function testFindByAlias()
@ -202,7 +209,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$repos = $this->_em->getRepository('CMS:CmsUser');
$users = $repos->findAll();
$this->assertEquals(2, count($users));
$this->assertEquals(3, count($users));
}
/**
@ -284,10 +291,11 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
public function testFindMagicCallByNullValue()
{
$this->loadFixture();
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$this->setExpectedException('Doctrine\ORM\ORMException');
$users = $repos->findByStatus(null);
$this->assertEquals(1, count($users));
}
/**
@ -389,7 +397,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
/**
* @group DDC-1087
*/
public function testIsNullCriteria()
public function testIsNullCriteriaDoesNotGenerateAParameter()
{
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$users = $repos->findBy(array('status' => null, 'username' => 'romanb'));
@ -399,6 +407,16 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals(array('romanb'), $params);
}
public function testIsNullCriteria()
{
$this->loadFixture();
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$users = $repos->findBy(array('status' => null));
$this->assertEquals(1, count($users));
}
/**
* @group DDC-1094
*/
@ -411,7 +429,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$users1 = $repos->findBy(array(), null, 1, 0);
$users2 = $repos->findBy(array(), null, 1, 1);
$this->assertEquals(2, count($repos->findBy(array())));
$this->assertEquals(3, count($repos->findBy(array())));
$this->assertEquals(1, count($users1));
$this->assertEquals(1, count($users2));
$this->assertNotSame($users1[0], $users2[0]);
@ -428,10 +446,10 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$usersAsc = $repos->findBy(array(), array("username" => "ASC"));
$usersDesc = $repos->findBy(array(), array("username" => "DESC"));
$this->assertEquals(2, count($usersAsc), "Pre-condition: only two users in fixture");
$this->assertEquals(2, count($usersDesc), "Pre-condition: only two users in fixture");
$this->assertSame($usersAsc[0], $usersDesc[1]);
$this->assertSame($usersAsc[1], $usersDesc[0]);
$this->assertEquals(3, count($usersAsc), "Pre-condition: only three users in fixture");
$this->assertEquals(3, count($usersDesc), "Pre-condition: only three users in fixture");
$this->assertSame($usersAsc[0], $usersDesc[2]);
$this->assertSame($usersAsc[2], $usersDesc[0]);
}