Reputation: 705
I'm using Yii2 Advanced and I'm getting this error
Trying to get property 'chnmem_stid' of non-object
The error is in this function in $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,
]);
}
the function AxChnPermission::ChnMember($model->channel_id); is
public static function ChnMember($chn_id)
{
$member = ChnMember::findOne(['user_id' => Yii::$app->user->id, 'channel_id' => $chn_id]);
return $member;
}
so the function I want to return only one result, The "chnmem_stid" is set to hasOne in model
/**
* 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']);
}
The error appears if return of this function is NULL
public static function ChnMember($chn_id)
{
$member = ChnMember::findOne(['user_id' => Yii::$app->user->id, 'channel_id' => $chn_id]);
return $member;
}
Upvotes: 0
Views: 52
Reputation: 22174
Your function AxChnPermission::ChnMember()
may return null
(if requested record does not exist). And this is probably the case, since error message says that $isMember
is not object. You need to make additional check for this case:
$isMember = AxChnPermission::ChnMember($model->channel_id);
if ($isMember === null) {
// throw exception?
}
Upvotes: 1