I have used http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/ tutorial and its great.
Everything is working fine but I get stuck after adding "Scenario 3 Steps":
// filter by parent name
$query->joinWith(['parent' => function ($q) {
$q->where('parent.first_name LIKE "%' . $this->parentName . '%" ' .
'OR parent.last_name LIKE "%' . $this->parentName . '%"');
}]);
it fires mysql query like:
SELECT COUNT(*) FROM `person` LEFT JOIN `country` ON
`person`.`country_id` = `country`.`id` LEFT JOIN `person` `parent` ON
`person`.`id` = `parent`.`parent_id` WHERE parent.first_name LIKE "%%" OR
parent.last_name LIKE "%%"
Which doesn't return any records.
I have tried something like:
if ($this->parentName) {
$query->joinWith(['parent' => function ($q) {
$q->where('parent.first_name LIKE "%' . $this->parentName . '%" ' .
'OR parent.last_name LIKE "%' . $this->parentName . '%"');
}]);
}else {
$query->joinWith('parent');
}
but that's giving me an error like:
Trying to get property of non-object
1. in /var/www/html/advanced/common/models/Person.php at line 54
/* Getter for parent name */
public function getParentName() {
return $this->parent->fullName; // its 54th line
}