8.4.2 How to join 3 tables for search by each column

Permalink
I have 3 tables: Countries, States, Cities. They have OneToMany relationships Countries->Cities, States->Cities.

I have the following in the CityList:
if ($this->search) {
    $query->innerJoin('c', 'Countries', 'co', 'co.country_id = c.country')
        ->innerJoin('c', 'States', 'st', 'st.state_id = c.state')
        ->andWhere('co.country like ?')->setParameter($paramcount++, '%'. $this->search. '%')
        ->orWhere('st.state like ?')->setParameter($paramcount++, '%'. $this->search. '%')
        ->orWhere('city like ?')->setParameter($paramcount++, '%'. $this->search. '%');
}
return $query;

This works fine, it searches for either a country or a state or a city but ONLY if the country AND state are NOT empty. And I can't figure out why it wouldn't search without them just by the city.

Does anyone have any ideas?

linuxoid
 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
At a guess, you need a left join, not an inner join
linuxoid replied on at Permalink Reply
linuxoid
This worked. Thank you very much!