8000 [Form] Fixed: Filter non-integers when selecting entities by int ID by webmozart · Pull Request #14950 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed: Filter non-integers when selecting entities by int ID #14950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Form] Fixed: Filter non-integers when selecting entities by int ID
  • Loading branch information
webmozart committed Jun 12, 2015
commit 352be8e571c6b6759c362af2fd8fb9f2bae0496d
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public function getEntitiesByIds($identifier, array $values)
$metadata = $qb->getEntityManager()->getClassMetadata($entity);
if (in_array($metadata->getTypeOfField($identifier), array('integer', 'bigint', 'smallint'))) {
$parameterType = Connection::PARAM_INT_ARRAY;

// Filter out non-integer values (e.g. ""). If we don't, some
// databases such as PostgreSQL fail.
$values = array_values(array_filter($values, function ($v) {
return (string) $v === (string) (int) $v;
}));
} else {
$parameterType = Connection::PARAM_STR_ARRAY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Doctrine\DBAL\Connection;

class ORMQueryBuilderLoaderTest extends DoctrineTestHelper
class ORMQueryBuilderLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
Expand All @@ -27,6 +27,7 @@ public function testItOnlyWorksWithQueryBuilderOrClosure()

/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @group legacy
*/
public function testClosureRequiresTheEntityManager()
{
Expand All @@ -47,7 +48,7 @@ public function testIdentifierTypeIsIntegerArray()

protected function checkIdentifierType($classname, $expectedType)
{
$em = $this->createTestEntityManager();
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder('QueryMock')
->setMethods(array('setParameter', 'getResult', 'getSql', '_doExecute'))
Expand All @@ -56,7 +57,7 @@ protected function checkIdentifierType($classname, $expectedType)
$query->expects($this->once())
->method('setParameter')
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', array(), $expectedType)
->will($this->returnValue($query));
->willReturn($query);

$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs(array($em))
Expand All @@ -65,12 +66,41 @@ protected function checkIdentifierType($classname, $expectedType)

$qb->expects($this->once())
->method('getQuery')
->will($this->returnValue($query));
->willReturn($query);

$qb->select('e')
->from($classname, 'e');

$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id', array());
}

public function testFilterNonIntegerValues()
{
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder('QueryMock')
->setMethods(array('setParameter', 'getResult', 'getSql', '_doExecute'))
->getMock();

$query->expects($this->once())
->method('setParameter')
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', array(1, 2, 3), Connection::PARAM_INT_ARRAY)
->willReturn($query);

$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
->setConstructorArgs(array($em))
->setMethods(array('getQuery'))
->getMock();

$qb->expects($this->once())
->method('getQuery')
->willReturn($query);

$qb->select('e')
->from('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity', 'e');

$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id', array(1, '', 2, 3, 'foo'));
}
}
0