change the insert code when form is created

hi!! I have a particular model called products,in which when i create a new product i want to change the insert which goes. actually when one product is created i also want make the insert in someother table.

just want to know where to code the queries…lik which file??Thanks

You want to add a new entry in a table as soon as a product is created and saved in the database?

You can add a function to your product model called afterSave() which is overriding the CActiveRecord afterSave() method


public function afterSave()

{

   $second_model=new SecondModelClass;

   $second_model->name='2nd model';

   $second_model->description='I am added after a model was successfully saved';

   $second_model->save();

   parent::afterSave();

}

Hi Haensel,

I managed to make the FK inserts in another table but i want to also insert some other data in the same table. How so i go about that? If i have to use aftersave can u please elaboraste a little more. I am very new at yii.

Thanks

In that case just use $this which refers to the current model instance to which you want to add additional data but this time do it in the beforeSave method:


public function beforeSave()

{

   if(parent::beforeSave()){

      $this->attribute1 = "someval";

      $this->attribute2 = "someval";

      return true;

   }

   return false;

}



create an object of a different model and in that action and assign a attributes which you want to insert in that model…then will be save easily in that model also…

for eg: $modelnew= new $extradataModel;

     assign attribute like: 


      


      $modelnew->data1=//assin what you want.


      $modelnew->data2=//assign what you want.





    and then save this model with $modelnew->save();

your data will definitely saved in that model also.