Skip to content
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

Added addParameters() to Query and QueryBuilder #512

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/Doctrine/ORM/AbstractQuery.php
Expand Up @@ -240,6 +240,48 @@ function ($parameter) use ($key)
return $this;
}

/**
* Adds a collection of query parameters, overriding any existing positions/names.
* Behaves like setParameters() before 2.3.
*
* @param \Doctrine\Common\Collections\ArrayCollection|array $parameters
*
* @return \Doctrine\ORM\AbstractQuery This query instance.
*/
public function addParameters($parameters)
{
$currentNames = $this->parameters->map(
function ($parameter)
{
return $parameter->getName();
}
);
$currentNames = $currentNames->toArray();

foreach ($parameters as $key => $value) {
if ($value instanceof Query\Parameter) {
// $value is a Query\Parameter
$parameter = $value;

if (false !== ($index = array_search($parameter->getName(), $currentNames))) {
$this->parameters[$index]->setValue($parameter->getValue(), $parameter->getType());
} else {
$this->parameters->add($parameter);
}

} else {
// $value is an actual value, $key is its position/name
if (false !== ($index = array_search($key, $currentNames))) {
$this->parameters[$index]->setValue($value);
} else {
$this->parameters->add(new Query\Parameter($key, $value));
}
}
}

return $this;
}

/**
* Process an individual parameter value
*
Expand Down
57 changes: 57 additions & 0 deletions lib/Doctrine/ORM/QueryBuilder.php
Expand Up @@ -416,6 +416,63 @@ public function setParameters($parameters)
return $this;
}

/**
* Adds a collection of query parameters, overriding any existing positions/names.
* Behaves like setParameters() before 2.3.
*
* <code>
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->from('User', 'u')
* ->where('u.id = :user_id1 OR u.id = :user_id2 OR u.id = :user_id3' OR u.id = :user_id4)
* ->addParameters(new ArrayCollection(array(
* new Parameter('user_id1', 1),
* new Parameter('user_id2', 2)
* )))
* ->addParameters(new ArrayCollection(array(
* new Parameter('user_id3', 3),
* new Parameter('user_id4', 4)
* )));
* </code>
*
* @param \Doctrine\Common\Collections\ArrayCollection|array $parameters
*
* @return \Doctrine\ORM\QueryBuilder This QueryBuilder instance.
*/
public function addParameters($parameters)
{
$currentNames = $this->parameters->map(
function ($parameter)
{
return $parameter->getName();
}
);
$currentNames = $currentNames->toArray();

foreach ($parameters as $key => $value) {
if ($value instanceof Query\Parameter) {
// $value is a Query\Parameter
$parameter = $value;

if (false !== ($index = array_search($parameter->getName(), $currentNames))) {
$this->parameters[$index]->setValue($parameter->getValue(), $parameter->getType());
} else {
$this->parameters->add($parameter);
}

} else {
// $value is an actual value, $key is its position/name
if (false !== ($index = array_search($key, $currentNames))) {
$this->parameters[$index]->setValue($value);
} else {
$this->parameters->add(new Query\Parameter($key, $value));
}
}
}

return $this;
}

/**
* Gets all defined query parameters for the query being constructed.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/QueryTest.php
Expand Up @@ -49,6 +49,49 @@ public function testSetParameters()
$this->assertEquals($parameters, $query->getParameters());
}

public function testAddParameters()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");

// Add 2 initial parameters as ArrayCollection containing Parameter objects.
$parameters = new ArrayCollection();
$parameters->add(new Parameter(1, 'foo'));
$parameters->add(new Parameter(2, 'bar'));

$query->addParameters($parameters);

$this->assertEquals($parameters, $query->getParameters());

// Add 2 other parameters as array containing key/value pairs.
// The first parameter should override an existing one, the second parameter is new.
$query->addParameters(array(
1 => 'foobar',
3 => 'barfoo'
));

$parameters = $query->getParameters();

$this->assertEquals(3, count($parameters));
$this->assertEquals('foobar', $parameters[0]->getValue());
$this->assertEquals('bar', $parameters[1]->getValue());
$this->assertEquals('barfoo', $parameters[2]->getValue());

// Add 2 more parameters as ArrayCollection containing Parameter objects.
// The first parameter should override an existing one, the second parameter is new.
$query->addParameters(new ArrayCollection(array(
new Parameter(2, 'foobarfoo'),
new Parameter(4, 'barfoobar')
)));

$parameters = $query->getParameters();

$this->assertEquals(4, count($parameters));
$this->assertEquals('foobar', $parameters[0]->getValue());
$this->assertEquals('foobarfoo', $parameters[1]->getValue());
$this->assertEquals('barfoo', $parameters[2]->getValue());
$this->assertEquals('barfoobar', $parameters[3]->getValue());
}

public function testFree()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
Expand Down
46 changes: 46 additions & 0 deletions tests/Doctrine/Tests/ORM/QueryBuilderTest.php
Expand Up @@ -463,6 +463,52 @@ public function testSetParameters()
$this->assertEquals($parameters, $qb->getQuery()->getParameters());
}

public function testAddParameters()
{
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->where($qb->expr()->orx('u.username = :username', 'u.username = :username2'));

// Add 2 initial parameters as ArrayCollection containing Parameter objects.
$parameters = new ArrayCollection();
$parameters->add(new Parameter('username', 'foo'));
$parameters->add(new Parameter('username2', 'bar'));

$qb->addParameters($parameters);

$this->assertEquals($parameters, $qb->getParameters());

// Add 2 other parameters as array containing key/value pairs.
// The first parameter should override an existing one, the second parameter is new.
$qb->addParameters(array(
'username' => 'foobar',
'username3' => 'barfoo'
));

$parameters = $qb->getParameters();

$this->assertEquals(3, count($parameters));
$this->assertEquals('foobar', $parameters[0]->getValue());
$this->assertEquals('bar', $parameters[1]->getValue());
$this->assertEquals('barfoo', $parameters[2]->getValue());

// Add 2 more parameters as ArrayCollection containing Parameter objects.
// The first parameter should override an existing one, the second parameter is new.
$qb->addParameters(new ArrayCollection(array(
new Parameter('username2', 'foobarfoo'),
new Parameter('username4', 'barfoobar')
)));

$parameters = $qb->getParameters();

$this->assertEquals(4, count($parameters));
$this->assertEquals('foobar', $parameters[0]->getValue());
$this->assertEquals('foobarfoo', $parameters[1]->getValue());
$this->assertEquals('barfoo', $parameters[2]->getValue());
$this->assertEquals('barfoobar', $parameters[3]->getValue());
}


public function testGetParameters()
{
Expand Down