4

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

Like:

$results = FooQuery::create()->joinBar()->filterByBarSurname('surname');
Tower
  • 98,741
  • 129
  • 357
  • 507

1 Answers1

7

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();
j0k
  • 22,600
  • 28
  • 79
  • 90
  • Just a quick question. What if you can't call that method and have to use `join()`? – Tower Jun 26 '12 at 12:50
  • 1
    Well then you will have to use the old way with `where` instead of `filterByXXX`. – j0k Jun 26 '12 at 12:55
  • There shouldn't be a time where you can't use a table query object unless you've made a mistake in your schema in relating tables that should be related. You can use these query objects at any depth by cascading the use statements. – Ryan Rentfro Dec 30 '15 at 16:42