我正在使用Yii2 Advanced,但出现此错误
Trying to get property 'chnmem_stid' of non-object
$ isMember-> chnmem_stid中的此函数中有错误;
public function actionChannel($id)
{
$model = $this->findModelUID($id);
$isMember = AxChnPermission::ChnMember($model->channel_id);
$memberStt = array(1,2,3);
if (in_array($isMember->chnmem_stid, $memberStt))
{
$dataProviderPost = AxChannelProvider::ContentProviderMember ($model->channel_id);
}
else
{
$dataProviderPost = AxChannelProvider::ContentProviderGuest ($model->channel_id);
}
return $this->render('/channel/_viewPost', [
'model' => $this->findModelUID($id),
'isMember' => $isMember,
'dataProviderPost' => $dataProviderPost,
]);
}
函数AxChnPermission :: ChnMember($ model-> channel_id);是
public static function ChnMember($chn_id)
{
$member = ChnMember::findOne(['user_id' => Yii::$app->user->id, 'channel_id' => $chn_id]);
return $member;
}
所以我只想返回一个结果的函数,“ chnmem_stid”在模型中设置为hasOne
/**
* This is the model class for table "chnmember".
*
* @property string $member_note
* @property int $user_id
* @property int $channel_id
* @property int $channel_admin
* @property int $chnmem_stid
* @property string $chnmem_date
* @property int $dsh_statut
*
* @property Channel $channel
* @property User $user
* @property ChnmemberStatut $chnmemSt
* @property Channel $channelAdmin
*/
/**
* @return \yii\db\ActiveQuery
*/
public function getChnmemSt()
{
return $this->hasOne(ChnmemberStatut::className(), ['chnmem_stid' => 'chnmem_stid']);
}
如果此函数的返回为NULL,则会出现错误
public static function ChnMember($chn_id)
{
$member = ChnMember::findOne(['user_id' => Yii::$app->user->id, 'channel_id' => $chn_id]);
return $member;
}
答案 0 :(得分:1)
您的函数AxChnPermission::ChnMember()
可能返回null
(如果请求的记录不存在)。可能是这种情况,因为错误消息指出$isMember
不是对象。您需要对此情况进行其他检查:
$isMember = AxChnPermission::ChnMember($model->channel_id);
if ($isMember === null) {
// throw exception?
}