Automatically getting a query instead of a result set in Symfony
In my schema, a user is related to their questions.
I want to display all questions asked by a user.
I also want this page to be paginated using sfDoctrinePager
which requires a Doctrine_Query
as a parameter.
I know I can call $my_user->getQuestions()
to get all questions from my_user, but is there a way to get the query to get all these questions instead? Something like $my开发者_运维百科_user->getQuestionsQuery()
for example.
Or do I have to implement it myself?
It seems that you have to create a Doctrine_Query
object manually:
$query = new Doctrine::getTable('Question')->createQuery('q')
->where('q.uid = ?', $my_user->getId());
And then pass it to pager:
$this->pager = new sfDoctrinePager('Question', $max_per_page);
$this->pager->setQuery($query);
$this->pager->setPage($cur_page);
$this->pager->init();
Congratulations! You have ready to use specialized queries. These are just descendants of Doctrine_Query
but with your own shortcut methods. There's article written by Nicolas Perriault called "Optimize your Doctrine Workflow with Specialized Queries", which describes this technique more verbosely.
精彩评论