Yii 中的动作 Update() 来更新另一个模型的更新

发布于 2025-01-05 12:32:31 字数 1251 浏览 0 评论 0原文

我有这样的数据库

 === Invoice ===
 id
 customer_id (FK)
 description

 === Customer ===
 id
 firstname
 lastname

,我有两种形式的多模型,以便 Cstomer 表将加载到发票中。这样我就可以从一个视图轻松访问这两个模型。为此,我在两个模型中建立了关系,就像这样 在发票模型中,关系是这样的

  public function relations()
  {
    return array(
    'customer'    =>  array(self::BELONGS_TO,'Customer','customer_id'),
    );
  }

在客户模型中,关系是这样的

public function relations()
  {
    return array(
      'invoice' => array(self::HAS_MANY, 'Invoices','customer_id')
    );
  }

一切都工作正常。但是当我在发票控制器文件中进行 actionUpdate() 时,没有定义客户模型。所以我这样定义它

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

显示为未定义偏移量:0。我想在 ('customer_id'=>$_GET['id']) 中显示 id 的值,以便我可以轻松显示和更新 的值每个 ID。 如果我给出这样的值,

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

它很容易显示客户 ID 的值。那么如何获得这些值呢?任何帮助和建议都将非常有价值。

I have the database like this

 === Invoice ===
 id
 customer_id (FK)
 description

 === Customer ===
 id
 firstname
 lastname

I have multimodel for both the form so that Cstomer table will be load in Invoice. So that I can easily access the two models from a single view. For that I have made relation in both models just like this
In Invoice model the realtion is like this

  public function relations()
  {
    return array(
    'customer'    =>  array(self::BELONGS_TO,'Customer','customer_id'),
    );
  }

In Customer Model the relation is like this

public function relations()
  {
    return array(
      'invoice' => array(self::HAS_MANY, 'Invoices','customer_id')
    );
  }

Everything is working fine.But when I am going for actionUpdate() in Invoice controller file there is Customer model is not defined. So I made it define like this

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

It is showing as Undefined offset: 0. I want here in ('customer_id'=>$_GET['id']) the value of id so that I can easily show and update the values for each ids.
If I am giving the value like this

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

It is easily showing the value from Customer id. So how to get those values?Any help and suggestions will be highly appriciable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨敛清荷 2025-01-12 12:32:31

试试这个

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

Try this

public function actionView($id)
  {
    $model = $this->loadModel($id);
    $this->render('view',array(
      'model'=>$model,
      'customers'=>Customers::model()->findByPk($model->customer_id);
    ));
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文