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

Replace query builder with DQL #143

Merged
merged 1 commit into from
Aug 19, 2015
Merged

Conversation

bocharsky-bw
Copy link
Contributor

The select and from methods are optional

@javiereguiluz
Copy link
Member

Even if this method call is not strictly necessary, don't you think it improves code readability? Removing it looks like "magic" and Symfony always prefer "explicit". What do you think?

@bocharsky-bw
Copy link
Contributor Author

Yes, it looks a bit magically. However with select there is still without from, I think it could a bit confused too. As for me I don't like use select when fetch all entity.

I based this PR on Querying for Objects Using Doctrine's Query Builder but it's a tutorial, not best practices.

Also I surfed knpuniversity, Ryan don't use select with query builder in his screencasts: Go Pro with Doctrine Queries and Symfony2.

As for me, the ParamConverter do much more magic, however it's best practice :)

Do you think we should keep select?

@ghost
Copy link

ghost commented Aug 16, 2015

👍 I think we should remove select.

@xabbuh
Copy link
Member

xabbuh commented Aug 17, 2015

@javiereguiluz Why is the query builder used at all in this example? Imho we can simply use a plain DQL query here as the query is not built based on some argument.

@javiereguiluz
Copy link
Member

@xabbuh yes, I think DQL should be used in this example. By the way, anyone knows a good reference in the official Doctrine documentation that explains when to use each of their different ways to build queries? I'd like to add a help note linking to that article. Thanks.

@bocharsky-bw
Copy link
Contributor Author

@javiereguiluz I think Symfony docs has nice explanation about quering with DQL vs Query Builder.

@javiereguiluz
Copy link
Member

Honestly, I find the explanation very lacking. For example, when they explain the "query builder":

Instead of writing a DQL string, you can alternatively use a helpful object called the QueryBuilder to build that string for you:

But they never say why the query builder is better or worse and when to use it or avoid it.

@bocharsky-bw
Copy link
Contributor Author

It'll be nice to improve Symfony docs for this.

@bocharsky-bw bocharsky-bw changed the title Remove unnecessary method call Replace query builder with DQL Aug 18, 2015
@bocharsky-bw
Copy link
Contributor Author

The minor problem is that in PhpStorm I have autocompletion by property names with Query Builder what I haven't with DQL :(

FROM AppBundle:Post p
WHERE p.publishedAt <= :now
ORDER BY p.publishedAt DESC
")
Copy link
Member

Choose a reason for hiding this comment

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

Please use single quotes instead of double quotes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I'm interesting why does it better in this case?

Copy link
Member

Choose a reason for hiding this comment

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

It's the Symfony code style which we should imho follow here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for this explanation, I replaced quotes

@javiereguiluz
Copy link
Member

👍

@bocharsky-bw
Copy link
Contributor Author

@javiereguiluz What do you think about add next explanation?

Instead of writing a DQL string, you can alternatively use a helpful object called the QueryBuilder to build that string for you if you need to build a query based on some result of conditions. The QueryBuilder allow you to avoid manual query string concatenation by using suitable methods calls and do this concatenation for you. Also, with QueryBuilder you could easy take away some parts of query to separate methods and reuse it in other queries. Check more information about querying for objects in http://symfony.com/doc/current/book/doctrine.html#querying-for-objects

@javiereguiluz
Copy link
Member

@bocharsky-bw I think it's a very good starting point to make a pull request to the symfony-docs repo. My only concern is that phrases are too long :) But please, make the pull request and our cool doc managers will guide you to tweak the original text. Thanks!

@bocharsky-bw
Copy link
Contributor Author

I created PR symfony/symfony-docs#5637

@javiereguiluz
Copy link
Member

Thank you @bocharsky-bw.

@javiereguiluz javiereguiluz merged commit 83e5b9a into symfony:master Aug 19, 2015
javiereguiluz added a commit that referenced this pull request Aug 19, 2015
This PR was merged into the master branch.

Discussion
----------

Replace query builder with DQL

The `select` and `from` methods are optional

Commits
-------

83e5b9a Replace query builder with DQL
@bocharsky-bw bocharsky-bw deleted the patch-2 branch August 19, 2015 15:51
@Rasanga
Copy link
Contributor

Rasanga commented Sep 9, 2015

DQL has better performance over Query Builder?

@stof
Copy link
Member

stof commented Sep 9, 2015

@Rasanga the query builder will ultimately produce the DQL string. So if the query is not dynamic, you only perform extra work for no additional value if you build the string dynamically with many method calls.
The difference should be small though in most cases. And as soon as your DQL query becomes dynamic (adding some condition in the WHERE clause only in some cases for instance, or changing the sorting), it becomes much more maintainable to use the query builder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants