0

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,

5.1tat
  • 19
  • 1
  • 6
  • 1
    your storeName is null or cannot be found, check your relation getStoreName, and make sure it return something not null, you can try to trace it Yii::trace($this->storeName); to see if it's null – endrik exe Jan 31 '18 at 06:28

1 Answers1

1

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.

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • Now it throwing an error app\models\ has no relation named "storeNamesCombo". – 5.1tat Jan 31 '18 at 07:15
  • Oh, sorry, I missed that part. `getStoreNamesCombo` is not a relation and should not be called in `joinWith()`. Instead you should use `$query->joinWith(['author', 'storeName']);` – Bizley Jan 31 '18 at 07:18
  • Thanks, Changed that. The page loads good But the search box returns an error when i search something likes this SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "storenames" – 5.1tat Jan 31 '18 at 07:21
  • Is the database table that holds `storeName` relation data called `storeNames`? If this is typo in name just fix the `$query` conditions - if not, see if there is maybe something wrong with aliasing this table. BTW you should use proper query building for this `LIKE` query - right now it might be open for injection. – Bizley Jan 31 '18 at 07:28
  • Is the database table that holds storeName relation data called storeNames? Yes. – 5.1tat Jan 31 '18 at 07:30
  • Could you update you question with code of `getStoreName()` method? – Bizley Jan 31 '18 at 07:39
  • Hi thanks, but the query seems to be not working. SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: integer ~~ unknown LINE 1 The columns classId,platformId and familyId are intergers , as far as i know the tables are in 3NF. – 5.1tat Jan 31 '18 at 14:01
  • Still looking for an solution, it would be great if you can help @Bizley – 5.1tat Feb 01 '18 at 06:49
  • I'm sorry but it's difficult to help you based only on the code given - if the columns are integers why are you using `like` in the query? I guess you need to adjust these conditions. – Bizley Feb 01 '18 at 07:11
  • I removed the 'like' for integer columns but still the problem persists.!! – 5.1tat Feb 01 '18 at 07:15
  • You can use `like` for string columns, for integer you can build query with array [column => value] but it would be weird to use the same condition for both integer and string column so I guess you need to recreate the original condition and probably use two or more. – Bizley Feb 01 '18 at 07:32
  • Can you please post an example code based on my code ? I am completely new to programming. @Bizley – 5.1tat Feb 01 '18 at 08:38
  • I've edited my answer again. If this not helps I'm afraid you need to figure out the rest on your own because this is out of StackOverflow scope. – Bizley Feb 01 '18 at 10:20
  • Thanks, but i need columns to be merged and searched. This will only work if columns are seperate! – 5.1tat Feb 01 '18 at 11:06