title: Yii collecting data from two models into single form
body: <p>In    my    web    application   I   need   to  collect  data  from  two  models  in  a   single   form.   The   first  model    form   is   like   an   update   action   for   making changes   in     existing      data   and   the   second   form   is   for   creating   a   new     record. Below is    the      code      I     tried   but when I click "save"      button Its not saving and redirecting instead     it     is         just     staying in the same page and the  changes I made are reverting back to their previous  values for I model and second model the attribute are becoming empty.</p>
  
  <p>My code for  the controller</p>
  
  <pre><code>public function actionBookvegetable($id){
      $BookVegetable=new BookVegetable;
      $model=$this-&gt;loadModel($id);
  
          if(isset($_POST['ProducerOffer'])AND (isset($_POST['BookVegetable'])) ) {
  
  
  
          $model-&gt;attributes=$_POST['ProducerOffer'];
          $BookVegetable-&gt;attributes=$_POST['BookVegetable'];
          if($ProducerOffer-&gt;validate()  AND  $BookVegetable-&gt;validate()) {
              $BookVegetable-&gt;save();
  
  
  
          $BookVegetable-&gt;booked_by=Yii::app()-&gt;user-&gt;id;
          $BookVegetable-&gt;producer_offer_id=$model-&gt;id;
  
          $model-&gt;save();
  
          }
          if (($model-&gt;hasErrors() === false)&amp;&amp;($BookVegetable-&gt;hasErrors===false))
          {
              $this-&gt;redirect(Yii::app()-&gt;user-&gt;returnUrl);
            }
      }
          else
          {
  
              Yii::app()-&gt;user-&gt;setReturnUrl($_GET['returnUrl']);
          }
          $this-&gt;render('book',array('model'=&gt;$model,'BookVegetable'=&gt;$BookVegetable));
          }
  </code></pre>
  
  <p>My code for the view  form.</p>
  
  <pre><code>&lt;?php $form=$this-&gt;beginWidget('bootstrap.widgets.TbActiveForm',array('id'=&gt;'non-ajax_form','enableAjaxValidation'=&gt;false,)); ?&gt;
  &lt;p class="help-block"&gt;Fields with &lt;span class="required"&gt;*&lt;/span&gt; are required.&lt;/p&gt;
  
  &lt;?php echo $form-&gt;errorSummary($model,$BookVegetable); ?&gt;
  &lt;?php echo "&lt;br&gt;" ?&gt;
  
  &lt;?php echo $form-&gt;dropDownList($model,'vegetable_id', CHtml::listData(Vegetable::model()-&gt;findAll(),'id', 'name'), array('prompt'=&gt;'Select Vegetable')); ?&gt;
  
  
  &lt;?php echo CHtml::encode($model-&gt;getAttributeLabel('offered_qty')); ?&gt;
  
  &lt;?php echo CHtml::textField("offered_qty",$model-&gt;offered_qty,array('readonly'=&gt;true)); ?&gt;
  
  
  &lt;?php echo CHtml::encode($model-&gt;getAttributeLabel('unit_cost')); ?&gt;
  
  &lt;?php echo CHtml::textField("unit_cost",$model-&gt;unit_cost,array('readonly'=&gt;true)); ?&gt;
  
  &lt;?php echo CHtml::encode($model-&gt;getAttributeLabel('unit_delivery_cost')); ?&gt;
  
  
  &lt;?php echo CHtml::textField("unit_delivery_cost",$model-&gt;unit_delivery_cost,array('readonly'=&gt;true)); ?&gt;
  &lt;?php echo CHtml::textField("booked_quantity",$BookVegetable-&gt;booked_quantity); ?&gt;
  
  
  &lt;/div&gt;
  &lt;?php $this-&gt;endWidget(); ?&gt;
  &lt;/div&gt;  
  
  
  
  
  
  &lt;div class="form-actions"&gt;
      &lt;?php $this-&gt;widget('bootstrap.widgets.TbButton', array('buttonType'=&gt;'submit', 'type'=&gt;'primary', 'label'=&gt;  'Save',)); ?
  </code></pre>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <p>How should I resolve this Anybody help me out .I am unable to debug the source of error</p>