Search code examples
phpsymfonypropel

Grouping filters in Propel


I currently have a Propel based query that looks like this:

$product = ProductQuery::create()
  ->filterByLive(1)
  ->filterByApproved(1)
  ->findOneByFilename($filename);

I regularly use this query and instead of having to chain the two filters I wondered whether it's possible to create a new filter that encapsulates them? This means that if I were to add an additional filter in future then I could simply do it in this single method rather than having to go through the entire project adding the new filter.

For example:

$product = ProductQuery::create()
  ->filterByIsActive()
  ->findOneByFilename($filename);

Is this possible?


Solution

  • You can create needed method in your ProductQuery:

    public function filterByIsActive()
    {
        return $this
                ->filterByLive(1)
                ->filterByApproved(1);
    }
    

    Propel generates this file only once and you can put any code you like inside this class - and it will not be overwritten.