Reputation: 19
I get this above mentioned error when i was trying to write a search and filter query for gridview,here is code:
Store Model: Relation
public function getStoreNamesCombo(){
return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName;
}
}
StoreSearch Model:
$query->joinWith(['author',
'storeNamesCombo']);
if(!empty($this->storeNamesCombo)){
$query->andWhere('storeNames.classId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.platformId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.familyId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.subFamilyName LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.variantName LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR CONCAT(storeNames.classId, " ", storeNames.platformId, " ", storeNames.familyId, " ", storeNames.subFamilyName, " ", storeNames.variantName) LIKE "%' . $this->storeNamesCombo . '%"'
);
Gridview
'value' => function($q) use ($storeFamilies, $storeClasses, $storePlatforms){
return implode('.', array_filter([
$storeClasses[$q->storeName->classId] ?? null,
$storePlatforms[$q->storeName->platformId] ?? null,
$storeFamilies[$q->storeName->familyId] ?? null,
$q->storeName->subFamilyName,
$q->storeName->variantName,
]));
Error Trying to get property of non-object
Error Line: Store Model Relation.
UPDATE:
public function getStoreName()
{
return $this->hasOne(StoreNames::className(), ['id' => 'storeNameId'])->via('transaction');
}
thanks in advance,
Upvotes: 0
Views: 255
Reputation: 18021
Make sure relation is fetched first to be safe.
public function getStoreNamesCombo()
{
if ($this->storeName) {
return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName;
}
return null;
}
EDIT:
BUT in your code one thing does not make sense. You are using value of getStoreNamesCombo()
as the condition for query searching through storeNames
table which is something like an inception idea.
I guess what you want to do is:
Set field in form model that will be filled with search term for the query condition.
public $storeNameSearch;
User fills that through the form for example. Now you can use it for the query.
if (!empty($this->storeNameSearch)){
$query->andWhere([
'or',
['like', 'storeNames.classId', $this->storeNameSearch],
['like', 'storeNames.platformId', $this->storeNameSearch],
['like', 'storeNames.familyId', $this->storeNameSearch],
['like', 'storeNames.subFamilyName', $this->storeNameSearch],
['like', 'storeNames.variantName', $this->storeNameSearch],
]);
}
BTW checking concatenated fields here seems redundant.
You can use getStoreNamesCombo()
(->storeNamesCombo
) on the prepared model in the gridview.
Edit 2:
If some of the columns are integers it sounds more reasonable to build conditions differently. If you can filter each column separately and there are filter fields user can fill it would be something like:
$query
->andFilterWhere(['storeNames.classId' => $this->storeClassId])
->andFilterWhere(['storeNames.platformId' => $this->storePlatformId])
->andFilterWhere(['storeNames.familyId' => $this->storeFamilyId])
->andFilterWhere(['like', 'storeNames.subFamilyName', $this->storeFamilyName])
->andFilterWhere(['like', 'storeNames.variantName', $this->storeVariantName]);
where first 3 columns are integers and last two are strings.
Upvotes: 1