Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在Yii2中获取非对象的属性_Yii2_Yii2 Advanced App_Yii2 Basic App_Yii2 Model - Fatal编程技术网

试图在Yii2中获取非对象的属性

试图在Yii2中获取非对象的属性,yii2,yii2-advanced-app,yii2-basic-app,yii2-model,Yii2,Yii2 Advanced App,Yii2 Basic App,Yii2 Model,我在尝试为gridview编写搜索和筛选查询时遇到上述错误,代码如下: 商店模式:关系 StoreSearch模型: 网格视图 错误 正在尝试获取非对象的属性 错误行:存储模型关系 更新: 提前感谢,确保首先获取关系以确保安全 public function getStoreNamesCombo() { if ($this->storeName) { return $this->storeName->classId . ' ' . $this->s

我在尝试为gridview编写搜索和筛选查询时遇到上述错误,代码如下:

商店模式:关系

StoreSearch模型:

网格视图

错误 正在尝试获取非对象的属性

错误行:存储模型关系

更新:


提前感谢,

确保首先获取关系以确保安全

public function getStoreNamesCombo()
{
    if ($this->storeName) {
        return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName; 
    }
    return null;
}
编辑: 但是在你的代码中有一件事是没有意义的。您正在使用getStoreNamesCombo的值作为通过storeNames表进行查询搜索的条件,这类似于一个初始概念

我想你想做的是:

在表单模型中设置字段,该字段将填充查询条件的搜索词

public $storeNameSearch;
例如,用户通过表单填写。现在您可以使用它进行查询

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],
    ]);
}
顺便说一句,在这里检查连接字段似乎是多余的

您可以在gridview中对准备好的模型使用getStoreNamesCombo->storeNamesCombo

编辑2:

如果某些列是整数,则以不同方式构建条件听起来更合理。如果您可以单独筛选每一列,并且用户可以填写筛选字段,则可能会出现如下情况:

$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]);

其中前三列是整数,后两列是字符串。

您的storeName为null或找不到,请检查您的关系getStoreName,并确保它返回的内容不为null,您可以尝试跟踪它Yii::trace$this->storeName;要查看它是否为nullNow,它会抛出一个错误app\models\没有名为storeNamesCombo的关系。哦,抱歉,我错过了那个部分。getStoreNamesCombo不是关系,不应在joinWith中调用。相反,您应该使用$query->joinWith['author','storeName'];谢谢,我改了。页面加载良好,但当我搜索类似以下SQLSTATE[42P01]的内容时,搜索框返回错误:未定义的表:7错误:缺少表storeName的子句条目是保存名为storeName的storeName关系数据的数据库表吗?如果这是名称输入错误,只需修复$query条件-如果不是,请查看此表的别名是否有问题。顺便说一句,您应该为这个类似的查询使用适当的查询构建—现在它可能是为注入而打开的。保存storeName关系数据的数据库表是否称为storeName?对
public function getStoreNamesCombo()
{
    if ($this->storeName) {
        return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName; 
    }
    return null;
}
public $storeNameSearch;
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],
    ]);
}
$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]);