【问题标题】:Update Multiple Models in yii更新 yii 中的多个模型
【发布时间】:2013-12-27 13:44:00
【问题描述】:

我的视图文件上有 3 个选项卡

第一个标签:学生

第二个标签:教育

第三个标签:就业

我的表结构:

学生桌:

+-------------+------------+--------------+------------+-------------+
|    id(PK)   |  lead_id   |  st_name     |   st_email |  st_phone   |
+-------------+------------+--------------+------------+-------------+

线索详情:

+-------------+-------------+
|    id(PK)   | lead_ref_id |  
+-------------+-------------+

教育

+-------------+------------+--------------+--------------+
|    id(PK)   |  lead_id   |education_type|education_year|
+-------------+------------+--------------+--------------+

就业:

+-------------+------------+---------------+---------------------+
|    id(PK)   |  lead_id   |Employment_type|Employment_experience|
+-------------+------------+---------------+---------------------+

当我去更新动作时,我必须根据lead_id显示数据

我的学生查看文件代码:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'lead-student-detail-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.

// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
    )); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>



<div class="row">
    <?php echo $form->labelEx($model,'st_name'); ?>
    <?php echo   $form->textField($model,'st_name',array('size'=>50,'maxlength'=>50)); ?>
    <?php echo $form->error($model,'st_name'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'st_email'); ?>
    <?php echo $form->textField($model,'st_email',array('size'=>50,'maxlength'=>50)); ?>
    <?php echo $form->error($model,'st_email'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'st_phone'); ?>
    <?php echo $form->textField($model,'st_phone'); ?>
    <?php echo $form->error($model,'st_phone'); ?>
</div>



    </div><!-- form -->

我的教育视图代码

 <div class="form">

 <?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'lead-target-education-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
  )); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary(array($target)); ?>


</div>

    <?php echo $form->labelEx($target,'education_type'); ?>
    <?php echo $form->textField($target,'education_type'); ?>
    <?php echo $form->error($target,'education_type'); ?>

  </div>    


</div>

    <?php echo $form->labelEx($target,'academic_year'); ?>
    <?php echo $form->textField($target,'academic_year[]'); ?>
    <?php echo $form->error($target,'academic_year'); ?>

  </div>

我应该如何编写动作更新代码,以便数据根据lead_id 显示,而不是通过主键id 并根据lead_id 获取更新。

评论更新

铅和教育表之间存在一对多的关系。

+-------------+------------+--------------+--------------+
|    id(PK)   |  lead_id   |education_type|education_year|
+-------------+------------+--------------+--------------+ 
|     1       |    1       |    10        |    2003      |
+-------------+------------+--------------+--------------+
|     2       |    1       |    12        |    2005      |
+-------------+------------+--------------+--------------+

【问题讨论】:

  • 每个标签都有不同的型号。对吗?
  • 是的,每个标签的模型都不同
  • 您是否在模型中使用了realations()。它使加入控制器变得更加容易,简而言之,您的控制器,您应该在主插入控制器中逐步插入和更新模型,然后如果一切正常,保存主控制器数据并重定向它。creating-and-updating-model-and-its-related-models...,@987654322 @
  • 感谢您的解决方案我有多个关于lead_id 的条目我如何获取有关lead_id 的所有值,因为 findByPk() 只获取一行并且 findAll() 函数获取值但它没有显示结果它抛出错误尝试获取非对象的属性
  • 请提供您当前的控制器代码,并附上更新时发布到控制器的数据转储。

标签: php join yii model multi-model-forms


【解决方案1】:

在我看来,您只需要进行查找即可。所以你最终会得到的是

$lead_id = 1;

$student = Student::model()->find(array('condition'=>'lead_id = :lead_id','params'=>array(':lead_id'=>$lead_id)));
$student->name = 'newvalue';
$student->save();

$education = Education::model()->find(array('condition'=>'lead_id = :lead_id','params'=>array(':lead_id'=>$lead_id)));
$education->education_year = 'newvalue';
$education->save();

$employment = Employment::model()->find(array('condition'=>'lead_id = :lead_id','params'=>array(':lead_id'=>$lead_id)));
$employment->employment_type = 'newvalue';
$employment->save();

这里我们使用 find 访问了三个模型。我们通过参数:lead_id 输入lead_id,这是绑定到lead_id 变量':lead_id'=&gt;$lead_id

然后我可以如图所示更改字段,使用$model-&gt;save();函数保存新信息。

如果这不是您想要的信息,请发表评论,我会根据您的建议进行更新。

【讨论】:

  • 我有多个关于学生教育的条目,例如
    id lead_id 教育类型 教育年份
    1 1 10日 2003
    2 1 12 th 2005
    带有lead_id 很常见,我已经尝试过查找,它只获取一行而不是另一行请帮助。
  • Education::model()-&gt;find更改为Education::model()-&gt;findAll,然后循环遍历$educatiom
  • 感谢解决方案,它在视图部分有效,但在更新页面中显示错误致命错误:在非对象上调用成员函数 getErrors()
  • 感谢它的解决方案
  • @nitin 不用担心,很高兴你把它整理好了。
猜你喜欢
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
相关资源
最近更新 更多