当我尝试使用ActiveDataProvider显示连接表( hasOne 连接)时出现以下错误:
PHP Notice – yii\base\ErrorException
Trying to get property of non-object
我的模特:
public function dataProvider()
{
return new ActiveDataProvider([
'query' => Currency::find()->with('country'),
'pagination' => [
'pageSize' => 20,
],
'sort' => [
'defaultOrder' => [
'allowed' => SORT_DESC,
]
],
]);
}
hasOne 连接:
public function getCountry()
{
return $this->hasOne(Countries::className(), ['country_code' => 'country_code']);
}
控制器:
public function actionManage()
{
return $this->render('manage', [
'dataProvider' => Currency::find()->dataProvider(),
]);
}
查看:
Pjax::begin(
['linkSelector' => 'a:not(.linksWithTarget)']
);
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'label' => 'Country',
'value' => function ($model) {
return $model->country->country_name;
},
],
],
]);
Pjax::end();
我在这里做错了什么?
答案 0 :(得分:0)
亲爱的先生,您首先需要拥有关系名称国家/地区,如果您在数据库中设置关系,GII可以为您构建关系。 如果不是你可以通过2找到简单地做到!(不推荐)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.scrollbar/0.2.11/jquery.scrollbar.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.scrollbar/0.2.11/jquery.scrollbar.js"></script>
<h1>Width and height in style tag</h1>
<div class="scrollcontainer1 inner-scrollbar">
jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu
jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu
</div>
<h1>Width and height in style attr</h1>
<div class="scrollcontainer2 inner-scrollbar" style="width:200px;height:200px">
jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu
jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu jniuniuiubiubiuiuhiuhiuuy hbubu ubiub ubiu uiiu
</div>