Merge pull request #153 from asm89/shesek-patch-1
Support NULL in EntityRepository's magic findBy and findOneBy methods
This commit is contained in:
commit
0b1f6e3539
@ -204,8 +204,7 @@ class EntityRepository implements ObjectRepository
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !isset($arguments[0])) {
|
if (empty($arguments)) {
|
||||||
// we dont even want to allow null at this point, because we cannot (yet) transform it into IS NULL.
|
|
||||||
throw ORMException::findByRequiresParameter($method.$by);
|
throw ORMException::findByRequiresParameter($method.$by);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ class CmsUser
|
|||||||
*/
|
*/
|
||||||
public $id;
|
public $id;
|
||||||
/**
|
/**
|
||||||
* @Column(type="string", length=50)
|
* @Column(type="string", length=50, nullable=true)
|
||||||
*/
|
*/
|
||||||
public $status;
|
public $status;
|
||||||
/**
|
/**
|
||||||
|
@ -38,12 +38,19 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$user2->status = 'dev';
|
$user2->status = 'dev';
|
||||||
$this->_em->persist($user2);
|
$this->_em->persist($user2);
|
||||||
|
|
||||||
|
$user3 = new CmsUser;
|
||||||
|
$user3->name = 'Benjamin';
|
||||||
|
$user3->username = 'beberlei';
|
||||||
|
$user3->status = null;
|
||||||
|
$this->_em->persist($user3);
|
||||||
|
|
||||||
$this->_em->flush();
|
$this->_em->flush();
|
||||||
|
|
||||||
$user1Id = $user->getId();
|
$user1Id = $user->getId();
|
||||||
|
|
||||||
unset($user);
|
unset($user);
|
||||||
unset($user2);
|
unset($user2);
|
||||||
|
unset($user3);
|
||||||
|
|
||||||
$this->_em->clear();
|
$this->_em->clear();
|
||||||
|
|
||||||
@ -189,7 +196,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
|
||||||
|
|
||||||
$users = $repos->findAll();
|
$users = $repos->findAll();
|
||||||
$this->assertEquals(2, count($users));
|
$this->assertEquals(3, count($users));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFindByAlias()
|
public function testFindByAlias()
|
||||||
@ -202,7 +209,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$repos = $this->_em->getRepository('CMS:CmsUser');
|
$repos = $this->_em->getRepository('CMS:CmsUser');
|
||||||
|
|
||||||
$users = $repos->findAll();
|
$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()
|
public function testFindMagicCallByNullValue()
|
||||||
{
|
{
|
||||||
$this->loadFixture();
|
$this->loadFixture();
|
||||||
|
|
||||||
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
|
||||||
|
|
||||||
$this->setExpectedException('Doctrine\ORM\ORMException');
|
|
||||||
$users = $repos->findByStatus(null);
|
$users = $repos->findByStatus(null);
|
||||||
|
$this->assertEquals(1, count($users));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -389,7 +397,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
/**
|
/**
|
||||||
* @group DDC-1087
|
* @group DDC-1087
|
||||||
*/
|
*/
|
||||||
public function testIsNullCriteria()
|
public function testIsNullCriteriaDoesNotGenerateAParameter()
|
||||||
{
|
{
|
||||||
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
|
||||||
$users = $repos->findBy(array('status' => null, 'username' => 'romanb'));
|
$users = $repos->findBy(array('status' => null, 'username' => 'romanb'));
|
||||||
@ -399,6 +407,16 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$this->assertEquals(array('romanb'), $params);
|
$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
|
* @group DDC-1094
|
||||||
*/
|
*/
|
||||||
@ -411,7 +429,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$users1 = $repos->findBy(array(), null, 1, 0);
|
$users1 = $repos->findBy(array(), null, 1, 0);
|
||||||
$users2 = $repos->findBy(array(), null, 1, 1);
|
$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($users1));
|
||||||
$this->assertEquals(1, count($users2));
|
$this->assertEquals(1, count($users2));
|
||||||
$this->assertNotSame($users1[0], $users2[0]);
|
$this->assertNotSame($users1[0], $users2[0]);
|
||||||
@ -428,10 +446,10 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$usersAsc = $repos->findBy(array(), array("username" => "ASC"));
|
$usersAsc = $repos->findBy(array(), array("username" => "ASC"));
|
||||||
$usersDesc = $repos->findBy(array(), array("username" => "DESC"));
|
$usersDesc = $repos->findBy(array(), array("username" => "DESC"));
|
||||||
|
|
||||||
$this->assertEquals(2, count($usersAsc), "Pre-condition: only two users in fixture");
|
$this->assertEquals(3, count($usersAsc), "Pre-condition: only three users in fixture");
|
||||||
$this->assertEquals(2, count($usersDesc), "Pre-condition: only two users in fixture");
|
$this->assertEquals(3, count($usersDesc), "Pre-condition: only three users in fixture");
|
||||||
$this->assertSame($usersAsc[0], $usersDesc[1]);
|
$this->assertSame($usersAsc[0], $usersDesc[2]);
|
||||||
$this->assertSame($usersAsc[1], $usersDesc[0]);
|
$this->assertSame($usersAsc[2], $usersDesc[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user