简体   繁体   中英

Update Multiple Models in yii

I have 3 tabs on my view file

First tab: student

Second tab: education

Third tab: employment

My Table Structure:

student table:

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

Lead Details:

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

Education

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

Employment:

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

When I go to the update action, I have to show data according to the lead_id

My student view file code:

<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 -->

My education view code

 <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>

How should i write the action update code so data shows according to the lead_id , not by the primary key id and get update according to the lead_id .

Update From Comment

There is a one to many relationship between lead and education tables.

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

It seems to me you just need to do a find. So what you will end up with is

$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();

Here we have accessed three models using find. We feed in the lead_id via the parameter :lead_id , this is the bound to the lead_id variable ':lead_id'=>$lead_id

Then I can change the fields as shown, using the $model->save(); function to save the new information.

If this is not the info you are after then please comment and I will update with your suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM