-1

I have a symfony application with several tables (or objects) that are associated with a place. Sometimes, throughout the app, I have to make some complex filtering (queries) to those objects based on the current place the user is navigating.

My idea, to avoid repeating a lot of code for several modules in the app, is to somehow override the execute() function from Doctrine_Query to apply the desired queries/filters when an extra parameter (the place value) was passed to it. I have no clue on how to achieve this, please help.

If you can achieve this easily using another approach, please share it as can't figure out any other way to do this.

pjamfaro
  • 23
  • 1
  • 6

1 Answers1

0

Well as long as you don't use the default queries (findById, etc.) you are using queries you've built in your tables and in that case you always use createQuery, that's the method you should overwrite (or create a new one). You also need to make sure to use ->andWhere afterwards.

Some pseudo-code to show what I mean:

Imagine you've got a model Tit. You then have a class TitTable -> Doctrine_Table Then create a table PlaceDoctrineTable and changes the inheritance: TitTable -> PlaceDoctrineTable -> Doctrine_Table

And that would be the new class:

class PlaceDoctrineTable extends Doctrine_Table {

 /**
  * @param ... @return Doctrine_Query
  */
  public function createPlaceSpecificQuery($alias)
  {
    $placeID = sfContext::getInstance()->... // you could use some service to set the place or placeID could be a parameter of this method

    $query = $this->createQuery($alias);
    $query->innerJoin("$alias.Place place")
     ->where('place.id = ?', $placeID)
    ;

    return $query;
  }

}

Then in the TitTable:

class TitTable extends PlaceDoctrineTable {

...

  public function getLatestTits($limit=1)
  {
    $query = $this->createPlaceSpecificQuery('t')
     ->andWhere('t....')
     ->...
    ;

    return $query->execute();
  }
}
François Constant
  • 5,531
  • 1
  • 33
  • 39
  • Hi. Thanks for your answer. I took a look at **createQuery** function and it seems to fit my needs. Now, 2 more questions regarding this. First, why should I override this one instead of execute, and second, what file structure should I create in order to override the function? – pjamfaro May 03 '13 at 15:39
  • Technically, it doesn't make a difference; I just think it makes more sense there. Actually, it would be better to have a new method called createQueryWithPlace() and just make sure you always use it. I'll complete my answer with some pseudo-code. – François Constant May 05 '13 at 02:59