Decrease number of queries for admin generated module: the better way

Posted: February 14, 2011 9:39 PM Updated: May 15, 2011 8:37 PM

Recently I saw two posts in the symfony blogosphere about decreasing number of queries in admin generated module (in January on Techie Corner, and today on Symfony world - which prompted me to write this post). And both bloggers seemed to be happy with overwriting buildQuery method of the module's action class. But generator.yml provides more elegant solution for admin generated modules.

When I first saw that solution on techiecorner, I thought: "hmmm.. that could be useful", but then, I looked at my generator.yml file and saw table_method i placed there long time ago...

YAML code
  1. generator:
  2.   param:
  3.     config:
  4.       list:
  5.         table_method: getAdminNotes

And while both, overwriting buildQuery and setting table_method can achieve same goal, I think table_method is more elegant, as it keeps your query in your ModelTable, and can be reused in other part of your project as well.

See The symfony Reference Book.

Grzegorz Śliwiński

Comments list (4)

Alan :LBO" Bem February 15, 2011 2:09 AM #1 replyreport
Gravatar photo
As a sidenote: buildQuery method can be left untouched even in case of dynamic criteria (eg. based on credentials) thanks to "admin.build_query" event.
fizyk February 16, 2011 10:27 PM #2 replyreport
Gravatar photo
That is true, events are great.
Tomasz Ducin May 12, 2011 2:36 PM #3 replyreport
Gravatar photo
I've juct came across your post corresponding to mine :) yes, you're right - generator.yml solution is more elegant. I can only excuse myself that both solutions achieve the same goal.

But I don't agree with your last sentence: "it keeps your query in your ModelTable, and can be reused in other part of your project as well" - you don't have to hardcode your query inside the buildQuery method, e.g. you may create a method that joins some specific models and can be reused in other parts of the project.
fizyk May 14, 2011 10:43 PM #4 replyreport
Gravatar photo
It's true, that you don't have to, but most programmers would do just that.
Add new comment

Your email will never be published

Must start with http:// or https://
Delete the reply