以 Yii 多模型形式获取第二个模型值时出错
我有这样的数据库
+------------------+
| Invoices |
+------------------+
| id |
| customer_id (Fk) |
| description |
+------------------+
+------------------+
| Customers |
+------------------+
| id |
| firstname |
| lastname |
| description |
+------------------+
所以根据我的要求我做了 multimodel。在 Invoices 模型中加载多模型 Customers 模型。现在发票控制器的 actionCreate() 是这样的
public function actionCreate()
{
$model=new Invoices;
$customers = new Customers;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Invoices'],$_POST['Customers']))
{
$model->attributes = $_POST['Invoices'];
$customers->attributes = $_POST['Customers'];
$valid = $model->validate();
$valid = $customers->validate();
if($valid)
{
$customers->save(false);
$model->customer_id = $customers->getPrimaryKey();
$model->save(false);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
'customers' => $customers,
));
}
actionView() 是看起来像现在
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,
));
}
,当我使用 renderpartial 渲染表单并像这样更改视图文件中的代码时,
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'customer_id',
array(
'label' => 'Firstname',
'value' => $customers->firstname,
),
array(
'label' => 'Lastname',
'value' => $customers->lastname,
),
array(
'label' => 'description',
'value' => $customers->description,
),
),
)); ?>
它显示了这样的错误尝试获取行 'value' => 中非对象的属性$customers->名字, 那么有人可以告诉我哪里出了问题吗?欢迎任何帮助和建议。
I have the database like this
+------------------+
| Invoices |
+------------------+
| id |
| customer_id (Fk) |
| description |
+------------------+
+------------------+
| Customers |
+------------------+
| id |
| firstname |
| lastname |
| description |
+------------------+
So as per my requirment I made multimodel.In that multimodel Customers model is loaded in Invoices model.Now the actionCreate() of invoice controller is like this
public function actionCreate()
{
$model=new Invoices;
$customers = new Customers;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Invoices'],$_POST['Customers']))
{
$model->attributes = $_POST['Invoices'];
$customers->attributes = $_POST['Customers'];
$valid = $model->validate();
$valid = $customers->validate();
if($valid)
{
$customers->save(false);
$model->customer_id = $customers->getPrimaryKey();
$model->save(false);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
'customers' => $customers,
));
}
The actionView() is looking like this
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,
));
}
Now when I rendered the form with renderpartial and changed my code in View file like this
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'customer_id',
array(
'label' => 'Firstname',
'value' => $customers->firstname,
),
array(
'label' => 'Lastname',
'value' => $customers->lastname,
),
array(
'label' => 'description',
'value' => $customers->description,
),
),
)); ?>
It showed an error like this Trying to get property of non-object in line 'value' => $customers->firstname,
So can someone tell me where the wrong part?Any help and suggestions are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为以下代码有问题:
尝试以下代码:
i think problem in follow code:
Try this code:
发票模型中与客户的关系的名称是什么?假设这是
海关
。您可以尝试以下操作:但是当然您必须将
customs
替换为关系的实际名称。What's the name of the relation to customers in the Invoices model? Let's say it's
customs
. You can try this:But of course you have to replace
customs
with the actual name of the relation.此错误表明您正在尝试访问非对象。最有可能的是它没有从您渲染它的地方填充。
请务必执行以下操作
echo CVarDumper($_GET['id'],100,true)
echo CVarDumper($customers,100,true)
并让我们知道是什么那里。
所以
actionView()
应该像这样显示它显示的内容。
步骤 2 将上面的代码撤消到原来的代码,然后添加
之前
,然后检查显示的内容
This error says that you are trying to access a non-object. Most probably it is not populated from where you are rendering it.
to be sure do following
echo CVarDumper($_GET['id'],100,true)
echo CVarDumper($customers,100,true)
and let us know what is there.
So The
actionView()
should look like thisshow what it displays.
step 2 undo above code to your orignal one and then add
before
and then check what gets displayed
findByAttributes 返回 null ,
如果
您可能仍然会遇到相同的错误。因此,在执行 $customers->firstname 之前,您必须检查 $customers 是否为空。
change
to
you might still get the same error if findByAttributes returns null. So you have to check if $customers is null or not before you do $customers->firstname.