Propel filtering based on joined tables columns?
Asked Answered
E

1

5

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

Like:

$results = FooQuery::create()->joinBar()->filterByBarSurname('surname');
Epirus answered 26/6, 2012 at 12:41 Comment(0)
R
8

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();
Reamonn answered 26/6, 2012 at 12:47 Comment(3)
Just a quick question. What if you can't call that method and have to use join()?Epirus
Well then you will have to use the old way with where instead of filterByXXX.Reamonn
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.Cavafy

© 2022 - 2024 — McMap. All rights reserved.