Search code examples
phppropel

Propel filtering based on joined tables columns?


How do I filter based on a joined table's columns in Propel?

Like:

$results = FooQuery::create()->joinBar()->filterByBarSurname('surname');

Solution

  • You have to use use method as described in the doc:

    $results = FooQuery::create()
      ->useBarQuery()
        ->filterBySurname('surname')
      ->endUse()
      ->find();
    
    // example Query generated for a MySQL database
    $query = 'SELECT foo.* from foo
    INNER JOIN bar ON foo.BAR_ID = bar.ID
    WHERE bar.SURNAME = :p1'; // :p1 => 'surname'
    

    If you have to use join(), I don't think you can use filterByXXX method but the old where:

    $results = FooQuery::create()
      ->join('Foo.Bar')
      ->where('Bar.surname = ?', 'surname')
      ->find();