以 Yii 多模型形式获取第二个模型值时出错

发布于 2025-01-06 06:59:52 字数 2311 浏览 1 评论 0原文

我有这样的数据库

+------------------+
|     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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独自唱情﹋歌 2025-01-13 06:59:52

我认为以下代码有问题:

public function actionView($id)
  {
    $this->render('view',array(
      'model'=>$this->loadModel($id),
      'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,
      ));
  }

尝试以下代码:

public function actionView($id)
  {
    $model = $this->loadModel($id);
    $this->render('view',array(
      'model'=>$model,
      'customers'=>Customers::model()->findByAttributes(array('id'=>$model->customer_id)),
      ));
  }

i think problem in follow code:

public function actionView($id)
  {
    $this->render('view',array(
      'model'=>$this->loadModel($id),
      'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,
      ));
  }

Try this code:

public function actionView($id)
  {
    $model = $this->loadModel($id);
    $this->render('view',array(
      'model'=>$model,
      'customers'=>Customers::model()->findByAttributes(array('id'=>$model->customer_id)),
      ));
  }
假情假意假温柔 2025-01-13 06:59:52

发票模型中与客户的关系的名称是什么?假设这是海关。您可以尝试以下操作:

  <?php $this->widget('zii.widgets.CDetailView', array(
  'data'=>$model,
  'attributes'=>array(
   'id',
   'customer_id',
    array(
      'label' => 'Firstname',
      'value' => $model->customs->firstname,
    ),
    array(
      'label' => 'Lastname',
      'value' => $model->customs->lastname,
    ),
    array(
      'label' => 'description',
      'value' => $model->customs->description,
    ),
  ),
)); ?>

但是当然您必须将 customs 替换为关系的实际名称。

What's the name of the relation to customers in the Invoices model? Let's say it's customs. You can try this:

  <?php $this->widget('zii.widgets.CDetailView', array(
  'data'=>$model,
  'attributes'=>array(
   'id',
   'customer_id',
    array(
      'label' => 'Firstname',
      'value' => $model->customs->firstname,
    ),
    array(
      'label' => 'Lastname',
      'value' => $model->customs->lastname,
    ),
    array(
      'label' => 'description',
      'value' => $model->customs->description,
    ),
  ),
)); ?>

But of course you have to replace customs with the actual name of the relation.

莳間冲淡了誓言ζ 2025-01-13 06:59:52

此错误表明您正在尝试访问非对象。最有可能的是它没有从您渲染它的地方填充。
请务必执行以下操作

  1. echo CVarDumper($_GET['id'],100,true)
  2. echo CVarDumper($customers,100,true)

并让我们知道是什么那里。

所以 actionView() 应该像这样显示

  public function actionView($id)
  {
    $this->render('view',array(
      'model'=>$this->loadModel($id),
      `echo CVarDumper($_GET['id'],100,true)`
      yii::app()->end();
      'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,
      ));
  }

它显示的内容。

步骤 2 将上面的代码撤消到原来的代码,然后添加

echo CVarDumper($customers,100,true)
yii::app()->end(); 

之前

<?php $this->widget('zii.widgets.CDetailView', array(.....

,然后检查显示的内容

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

  1. echo CVarDumper($_GET['id'],100,true)
  2. echo CVarDumper($customers,100,true)

and let us know what is there.

So The actionView() should look like this

  public function actionView($id)
  {
    $this->render('view',array(
      'model'=>$this->loadModel($id),
      `echo CVarDumper($_GET['id'],100,true)`
      yii::app()->end();
      'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,
      ));
  }

show what it displays.

step 2 undo above code to your orignal one and then add

echo CVarDumper($customers,100,true)
yii::app()->end(); 

before

<?php $this->widget('zii.widgets.CDetailView', array(.....

and then check what gets displayed

韵柒 2025-01-13 06:59:52

findByAttributes 返回 null ,

  'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,

如果

  'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id'])),

您可能仍然会遇到相同的错误。因此,在执行 $customers->firstname 之前,您必须检查 $customers 是否为空。

change

  'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id,

to

  'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id'])),

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文