L
L
Leopandro2016-11-15 13:57:40
Yii
Leopandro, 2016-11-15 13:57:40

What am I doing wrong when searching through related tables?

There is a Car model with a link to the brand by brand-id:

public function getBrand()
  {
    return $this->hasOne(Brand::className(), [
      'id' => 'brand_id'
    ]);
  }

This brand has a name field . And to search by the name
field , I do this:
$query->joinWith('brand');
$query->orFilterWhere([
  'like', 'brand.name', '%'.$this->company.'%', false
]);

(search by company field )
The code doesn't work. What am I doing wrong. If that the Brand model table is called car_brand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-11-15
@qonand

1. You now have the name of the model, but the name of the table should be
2. The characters '%' Yii sets itself

$query->orFilterWhere([
  'like', 'car_brand.name', $this->company
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question