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

Create queryBuilder from parts. #556

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions lib/Doctrine/ORM/EntityManager.php
Expand Up @@ -351,6 +351,28 @@ public function createQueryBuilder()
return new QueryBuilder($this);
}

/**
* Create a QueryBuilder instance from DQL parts
*
* @param array $parts
*
* @return QueryBuilder
*/
public function setDQLParts(array $parts = array())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What? Is this a joke? EntityManager::setDQLParts? EM doesn't have any DQL parts…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch! I really was thinking he applied the changes to the QueryBuilder.

{
$queryBuilder = new QueryBuilder($this);
foreach ($parts as $name => $part)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline before this foreach

{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix CS: newlines around logical blocks and PSR-2 generally

if ($part) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are empty parts even allowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDQLParts returns an array of all parts. It can have keys with an empty value.

if (is_array($part) && $name != 'join') {
$part = current($part);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be reset ?

}
$queryBuilder->add($name, $part);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline before this call

}
}
return $queryBuilder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline before return

}

/**
* Flushes all changes to objects that have been queued up to now to the database.
* This effectively synchronizes the in-memory state of managed objects with the
Expand Down
10 changes: 10 additions & 0 deletions tests/Doctrine/Tests/ORM/EntityManagerTest.php
Expand Up @@ -67,6 +67,16 @@ public function testCreateQueryBuilder()
$this->assertInstanceOf('Doctrine\ORM\QueryBuilder', $this->_em->createQueryBuilder());
}

public function testSetDQLParts()
{
$q = $this->_em->createQueryBuilder()
->select('u')
->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
->where('u.username = test');
$q2 = $this->_em->setDQLParts($q->getDQLParts());
$this->assertEquals($q->getDQL(), $q2->getDQL());
}

public function testCreateQueryBuilderAliasValid()
{
$q = $this->_em->createQueryBuilder()
Expand Down