Update()

Hello Friends…

   I Have 2 tables 1.add 2.maturity. In Addcreate form having 9 fields, on that 3 fileds are need to save in the maturity table, and rest of them in the add table.

This is the concept.

SO now I finish the create() in my Addcontroller. now the info will be save in the corresponding tables. Now i need to update.

my create() from addcontroller.


<?php


class AddController extends Controller

{

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

			'postOnly + delete', // we only allow deletion via POST request

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view','Dynamicheads','DynamicCategory','Dynamicbank','Dynamicintbank','view1'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}

	

	public function actionView1($id)

	{

	

	    $this->layout='/layouts/custom';

		$this->render('view1',array(

			'model'=>$this->loadModel($id),

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Add;

		$b=new Maturity;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		//if(isset($_POST['Add']))

		 if(isset($_POST['Add'], $_POST['Maturity']))

		{

			$model->attributes=$_POST['Add'];

			$b->attributes=$_POST['Maturity'];


			$maturitydate = $_POST['Maturity']['maturitydate'];

			$b->maturitydate = date('Y-d-m', strtotime($maturitydate));

			$model->attributes=$_POST['Add'];

			$startdate = $_POST['Maturity']['startdate'];

			$b->startdate = date('Y-d-m', strtotime($startdate));

			

			$valid=$model->validate();

        $valid=$b->validate() && $valid;

		if($valid)

        {

            // use false parameter to disable validation

            $model->save();

            $b->save();

			$this->redirect(array('admin'));

            // ...redirect to another page

        }

			

			//if($model->save())

			//	$this->redirect(array('view','id'=>$model->add_id));

		}


		$this->render('create',array(

			'model'=>$model,

			'b'=>$b,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

/*	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);

		


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Maturity'],$_POST['Add']))

		{

			$b->attributes=$_POST['Maturity'];

			$model->attributes=$_POST['Add'];

			if($model->save())

			{

			    if($b->save())

				{

				$this->redirect(array('view','id'=>$b->m_id,$model->add_id));

		}

}

		$this->render('update',array(

			'model'=>$model,

			'b'=>$b,

		));

	}

	}

	*/

	public function actionUpdate()

	{

 

    $model=$this->loadModel($id);

    $b = Maturity::model()->findByAttributes(array('code'=>$model->code));

 

     if(isset($_POST['Add'], $_POST['Maturity'])){

 

            $model->attributes=$_POST['Add'];

            $b->attributes=$_POST['Maturity'];

 

            $valid=$model->validate();

            $valid=$b->validate() && $valid;

 

              if($valid){

                if($model->save()){

     if(!empty($<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />){

 

        $b->code = $model-->code;

        $b->save();

      }

    $this->redirect(array('view','id'=>$model->id));

}

        }

 

        $this->render('update',array(

          'model'=>$model,

          'b'=>$b,

        ));

   }

}

	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'admin' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		$this->loadModel($id)->delete();


		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

		if(!isset($_GET['ajax']))

			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Add');

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Add('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Add']))

			$model->attributes=$_GET['Add'];


		$this->render('admin',array(

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer $id the ID of the model to be loaded

	 * @return Add the loaded model

	 * @throws CHttpException

	 */

	public function loadModel($id)

	{

		$model=Add::model()->findByPk($id);

		$originalDob = $model->maturitydate;

		$newDob = date("d-m-Y", strtotime($originalDob));

		$model->maturitydate=$newDob;

		




		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param Add $model the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='add-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

	

	public function actionDynamicCategory()

    {

        $catg_id = (int)$_GET[catg_id];

   		//$dept_id = (int)$_POST[Employee][dept_id]; 

        $datas=Heads::model()->findAll('catg_id=:catg_id',

	 			  array(':catg_id'=> $catg_id));

        $datas=CHtml::listData($datas,'head_id','heads');

                   

	    if($catg_id)

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select heads'),true); 

 	    } 

	    else

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select category '),true); 

	    }

        foreach($datas as $value=>$heades)

	    {

             echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($heades),true);

        }

    }

	

	public function actionDynamicheads()

    {

        $head_id = (int)$_GET[head_id];

   		//$dept_id = (int)$_POST[Employee][dept_id]; 

        $data=Subheads::model()->findAll('head_id=:head_id',

	 			  array(':head_id'=> $head_id));

        $data=CHtml::listData($data,'shead_id','subhead');

                   

	    if($head_id)

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select subheads'),true); 

 	    } 

	    else

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select heads '),true); 

	    }

        foreach($data as $value=>$sheades)

	    {

             echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($sheades),true);

        }

    }

	

	public function actionDynamicbank()

    {

        $bank_id = (int)$_GET[bank];

   		//$dept_id = (int)$_POST[Employee][dept_id]; 

        $bankdata=Branch::model()->findAll('bank_id=:bank_id',

	 			  array(':bank_id'=> $bank_id));

        $bankdata=CHtml::listData($bankdata,'branch_id','branch');

                   

	    if($bank_id)

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select branch'),true); 

 	    } 

	    else

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select bank 1st '),true); 

	    }

        foreach($bankdata as $value=>$banks)

	    {

             echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($banks),true);

        }

    }

	

	public function actionDynamicintbank()

    {

        $intbank_id = (int)$_GET[interest_bank];

   		//$dept_id = (int)$_POST[Employee][dept_id]; 

        $intbankdata=Branch::model()->findAll('bank_id=:bank_id',

	 			  array(':bank_id'=> $intbank_id));

        $intbankdata=CHtml::listData($intbankdata,'branch_id','branch');

                   

	    if($intbank_id)

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select branch'),true); 

 	    } 

	    else

	    {

	         echo CHtml::tag('option', 

	              array('value'=>0),CHtml::encode('Select bank 1st '),true); 

	    }

        foreach($intbankdata as $value=>$intbanks)

	    {

             echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($intbanks),true);

        }

    }

	

}



My model add.php


<?php


/**

 * This is the model class for table "add".

 *

 * The followings are the available columns in table 'add':

 * @property integer $add_id

 * @property string $code

 * @property integer $catg_id

 * @property integer $head_id

 * @property integer $shead_id

 * @property string $fdnumber

 * @property string $fdname

 * @property string $startdate

 * @property integer $noofyears

 * @property string $maturitydate

 * @property integer $amount

 * @property string $bank

 * @property string $branch

 * @property integer $rateofinterest

 * @property integer $interestamount

 * @property string $interest_bank

 * @property string $interest_branch

 * @property integer $annual

 */

class Add extends CActiveRecord

{

	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'add';

		return 'maturity';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('code, catg_id, head_id, shead_id, fdname, noofyears, amount, bank, branch, rateofinterest, interestamount, interest_bank, interest_branch, annual', 'required'),

			array('catg_id, head_id, shead_id, noofyears, amount, rateofinterest, interestamount, annual', 'numerical', 'integerOnly'=>false),

			//array('annual','integerOnly'=>true),

			array('code,fdnumber','unique'),

			array('code, fdnumber, fdname, bank, branch, interest_bank, interest_branch', 'length', 'max'=>255),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('add_id, code, catg_id, head_id, shead_id, fdnumber, fdname, startdate, noofyears, maturitydate, amount, bank, branch, rateofinterest, interestamount, interest_bank, interest_branch, annual', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'add_id' => 'Add',

			'code' => 'Code',

			'catg_id' => 'Catg',

			'head_id' => 'Head',

			'shead_id' => 'Shead',

			'fdnumber' => 'Fdnumber',

			'fdname' => 'Fdname',

			'startdate' => 'Startdate',

			'noofyears' => 'Noofyears',

			'maturitydate' => 'Maturitydate',

			'amount' => 'Amount',

			'bank' => 'Bank',

			'branch' => 'Branch',

			'rateofinterest' => 'Rateofinterest',

			'interestamount' => 'Interestamount',

			'interest_bank' => 'Interest Bank',

			'interest_branch' => 'Interest Branch',

			'annual' => 'Annual',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 *

	 * Typical usecase:

	 * - Initialize the model fields with values from filter form.

	 * - Execute this method to get CActiveDataProvider instance which will filter

	 * models according to data in model fields.

	 * - Pass data provider to CGridView, CListView or any similar widget.

	 *

	 * @return CActiveDataProvider the data provider that can return the models

	 * based on the search/filter conditions.

	 */

	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('add_id',$this->add_id);

		$criteria->compare('code',$this->code,true);

		$criteria->compare('catg_id',$this->catg_id);

		$criteria->compare('head_id',$this->head_id);

		$criteria->compare('shead_id',$this->shead_id);

		$criteria->compare('fdnumber',$this->fdnumber,true);

		$criteria->compare('fdname',$this->fdname,true);

		$criteria->compare('startdate',$this->startdate,true);

		$criteria->compare('noofyears',$this->noofyears);

		$criteria->compare('maturitydate',$this->maturitydate,true);

		$criteria->compare('amount',$this->amount);

		$criteria->compare('bank',$this->bank,true);

		$criteria->compare('branch',$this->branch,true);

		$criteria->compare('rateofinterest',$this->rateofinterest);

		$criteria->compare('interestamount',$this->interestamount);

		$criteria->compare('interest_bank',$this->interest_bank,true);

		$criteria->compare('interest_branch',$this->interest_branch,true);

		$criteria->compare('annual',$this->annual);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}


	/**

	 * Returns the static model of the specified AR class.

	 * Please note that you should have this exact method in all your CActiveRecord descendants!

	 * @param string $className active record class name.

	 * @return Add the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

	

	public function behaviors(){

        return array('ESaveRelatedBehavior' => array(

         'class' => 'application.components.ESaveRelatedBehavior')

     );

}

}



Help me out!!!!

show your maturity model also…

This is my maturity model code


<?php


/**

 * This is the model class for table "maturity".

 *

 * The followings are the available columns in table 'maturity':

 * @property integer $m_id

 * @property string $fdnumber

 * @property string $currentdate

 * @property string $startdate

 * @property string $maturitydate

 * @property integer $add_id

 * @property integer $r_id

 */

class Maturity extends CActiveRecord

{

	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'maturity';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			//array('fdnumber, currentdate, startdate, maturitydate', 'required'),

			array('add_id, r_id', 'numerical', 'integerOnly'=>true),

			array('fdnumber,code', 'length', 'max'=>200),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('m_id, fdnumber,code, currentdate, startdate, maturitydate, add_id, r_id', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'm_id' => 'M',

			'fdnumber' => 'Fdnumber',

			'code' => 'Code',

			'currentdate' => 'Currentdate',

			'startdate' => 'Startdate',

			'maturitydate' => 'Maturitydate',

			'add_id' => 'Add',

			'r_id' => 'R',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 *

	 * Typical usecase:

	 * - Initialize the model fields with values from filter form.

	 * - Execute this method to get CActiveDataProvider instance which will filter

	 * models according to data in model fields.

	 * - Pass data provider to CGridView, CListView or any similar widget.

	 *

	 * @return CActiveDataProvider the data provider that can return the models

	 * based on the search/filter conditions.

	 */

	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('m_id',$this->m_id);

		$criteria->compare('fdnumber',$this->fdnumber,true);

		$criteria->compare('code',$this->code,true);

		$criteria->compare('currentdate',$this->currentdate,true);

		$criteria->compare('startdate',$this->startdate,true);

		$criteria->compare('maturitydate',$this->maturitydate,true);

		$criteria->compare('add_id',$this->add_id);

		$criteria->compare('r_id',$this->r_id);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}


	/**

	 * Returns the static model of the specified AR class.

	 * Please note that you should have this exact method in all your CActiveRecord descendants!

	 * @param string $className active record class name.

	 * @return Maturity the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

	

	public function dates($currentdate,$maturitydate,$startdate)

	{

	   $this->currentdate=date("Y-m-d");

	   $this->maturitydate=date('Y-m-d', strtotime($maturitydate));

	   $this->startdatedate=date('Y-m-d', strtotime($startdate));

	}

	

}



You don’t having any relation between models Add & Maturity. why you are not use Primary key and Foreign Key between tables Add & Maturity, and then generate models. it’s logically best way to use both models else you can show manually.

Your view not render the data because of models relations, so now you need to use dataProvider.

follow this wiki - http://www.yiiframework.com/wiki/722/how-to-display-data-from-two-tables-in-tbgridview-using-dataprovider/

can you show me your view code?

Also are you able to display data in view from two tables after insert the records???