Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Create queryBuilder from parts. #556

Closed
wants to merge 3 commits into from

9 participants

@NoUseFreak

Add a feature that can create a queryBuilder from queryBuilder parts.

This can be used when a query is build and needs to be stored somehow and restore it later.

@doctrinebot
Collaborator

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DDC-2258

@Majkl578

:-1: This shouldn't pollute EntityManager's public API. QueryBuilder::setDQLParts() would be more appropriate, but still, there is QueryBuilder::add() method.

@NoUseFreak

@jmikola Just adding every part from getDQLParts won't work. This makes rebuilding a querybuilder hard.

lib/Doctrine/ORM/EntityManager.php
@@ -352,6 +352,28 @@ public function createQueryBuilder()
}
/**
+ * Create a QueryBuilder instance from DQL parts
+ *
+ * @param array $parts
+ *
+ * @return QueryBuilder
+ */
+ public function setDQLParts(array $parts = array())
+ {
+ $queryBuilder = new QueryBuilder($this);
+ foreach ($parts as $name => $part)
+ {
+ if ($part) {
+ if (is_array($part) && $name != 'join') {
+ $part = current($part);
@Ocramius Owner

Should this be reset ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/EntityManager.php
@@ -352,6 +352,28 @@ public function createQueryBuilder()
}
/**
+ * Create a QueryBuilder instance from DQL parts
+ *
+ * @param array $parts
+ *
+ * @return QueryBuilder
+ */
+ public function setDQLParts(array $parts = array())
+ {
+ $queryBuilder = new QueryBuilder($this);
+ foreach ($parts as $name => $part)
+ {
@Ocramius Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/EntityManager.php
@@ -352,6 +352,28 @@ public function createQueryBuilder()
}
/**
+ * Create a QueryBuilder instance from DQL parts
+ *
+ * @param array $parts
+ *
+ * @return QueryBuilder
+ */
+ public function setDQLParts(array $parts = array())
+ {
+ $queryBuilder = new QueryBuilder($this);
+ foreach ($parts as $name => $part)
@Ocramius Owner

Missing newline before this foreach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/EntityManager.php
((4 lines not shown))
+ * Create a QueryBuilder instance from DQL parts
+ *
+ * @param array $parts
+ *
+ * @return QueryBuilder
+ */
+ public function setDQLParts(array $parts = array())
+ {
+ $queryBuilder = new QueryBuilder($this);
+ foreach ($parts as $name => $part)
+ {
+ if ($part) {
+ if (is_array($part) && $name != 'join') {
+ $part = current($part);
+ }
+ $queryBuilder->add($name, $part);
@Ocramius Owner

Missing newline before this call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/EntityManager.php
((7 lines not shown))
+ *
+ * @return QueryBuilder
+ */
+ public function setDQLParts(array $parts = array())
+ {
+ $queryBuilder = new QueryBuilder($this);
+ foreach ($parts as $name => $part)
+ {
+ if ($part) {
+ if (is_array($part) && $name != 'join') {
+ $part = current($part);
+ }
+ $queryBuilder->add($name, $part);
+ }
+ }
+ return $queryBuilder;
@Ocramius Owner

Missing newline before return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/EntityManager.php
@@ -352,6 +352,28 @@ public function createQueryBuilder()
}
/**
+ * Create a QueryBuilder instance from DQL parts
+ *
+ * @param array $parts
+ *
+ * @return QueryBuilder
+ */
+ public function setDQLParts(array $parts = array())
+ {
+ $queryBuilder = new QueryBuilder($this);
+ foreach ($parts as $name => $part)
+ {
+ if ($part) {
@Ocramius Owner

Are empty parts even allowed?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/EntityManager.php
@@ -352,6 +352,28 @@ public function createQueryBuilder()
}
/**
+ * Create a QueryBuilder instance from DQL parts
+ *
+ * @param array $parts
+ *
+ * @return QueryBuilder
+ */
+ public function setDQLParts(array $parts = array())

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

@Ocramius Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@NoUseFreak

I moved it to the correct place, updated tests and applied coding standards.

@jmikola
Owner

@NoUseFreak: Why did you tag me in this issue? Did you mean to respond to @Majkl578?

@NoUseFreak

@jmikola I indeed ment @Majkl578 but the autocomplete apparently didn't go as planned.

@NoUseFreak

ping

@beberlei
Owner

@NoUseFreak why don't you do $qb2 = clone $qb;? Does the same thing

@beberlei
Owner

I don't think we want this feature, if you need to store a query you can store DQL instead.

@beberlei beberlei closed this
@L0rD59

Hey ! i need this feature too

Where i can find it ?

@stof

@L0rD59 this PR has been rejected. See the 2 comments from @beberlei just above

@guilhermeblanco

@L0rD59 You can easily achieve it:

$qb_new = clone $qb;
@L0rD59

thx for replies,

In detail, i use Lexik FormFilter Bundle and PagerFanta Bundle

and i mix both

so i want persist the QB of Lexik when changing page. I use session for that

but the QB Builder object can't persist in session, so i decide to persist QB->getParts()

I can too persist QB->getDQL()

Next, i need to transform QB->getParts() (or QB->getDQL()) to QB for PagerFanta bundle

thx for your times.

@stof

@L0rD59 Pagerfanta does not require you to use a query builder. It is able to paginate a Query object (actually, it only paginates Query objects as the first action done when using a QueryBuilder is doing a $query = $builder->getQuery()). And to build the Query object, the DQL is what you need

@L0rD59

@stof thx !

it's work with Query !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 25, 2013
  1. @NoUseFreak
  2. @NoUseFreak

    Update method name.

    NoUseFreak authored
Commits on Jan 28, 2013
  1. @NoUseFreak
This page is out of date. Refresh to see the latest.
View
30 lib/Doctrine/ORM/QueryBuilder.php
@@ -573,6 +573,36 @@ public function add($dqlPartName, $dqlPart, $append = false)
}
/**
+ * Set an array of DQL parts.
+ *
+ * <code>
+ * $qb = $em->createQueryBuilder()
+ * ->select('u', 'p')
+ * ->from('User', 'u')
+ * ->leftJoin('u.Phonenumbers', 'p');
+ * $qb2 = $em->createQueryBuilder()
+ * ->setDQLParts($qb->getDQLParts());
+ * </code>
+ *
+ * @param array $parts
+ *
+ * @return QueryBuilder This QueryBuilder instance.
+ */
+ public function setDQLParts(array $parts = array())
+ {
+ foreach ($parts as $name => $part) {
+ if ($part) {
+ if (is_array($part) && $name != 'join') {
+ $part = reset($part);
+ }
+ $this->add($name, $part);
+ }
+ }
+
+ return $this;
+ }
+
+ /**
* Specifies an item that is to be returned in the query result.
* Replaces any previously specified selections, if any.
*
View
12 tests/Doctrine/Tests/ORM/QueryBuilderTest.php
@@ -424,6 +424,18 @@ public function testAddCriteriaUndefinedLimit()
$this->assertEquals(10, $qb->getMaxResults());
}
+ public function testSetDQLParts()
+ {
+ $q = $this->_em->createQueryBuilder()
+ ->select('u')
+ ->from('Doctrine\Tests\Models\CMS\CmsUser', 'u')
+ ->where('u.username = test');
+
+ $q2 = $this->_em->createQueryBuilder()->setDQLParts($q->getDQLParts());
+
+ $this->assertEquals($q->getDQL(), $q2->getDQL());
+ }
+
public function testGetQuery()
{
$qb = $this->_em->createQueryBuilder()
Something went wrong with that request. Please try again.