Save(); Button How Does It Work?

Hello,

I am really not getting it. How does the save(); button work?

Right now I am using the controller to call a model in order to insert a new mysql record.

This is the code:





$fav = new favaffiliates;


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

		{

		$fav = favaffiliates::model()->find(array('select'=>'*'));

		$favaff->user_id="33";

		$fav->save();


		}



“user_id” is a table column and I’m calling the favaffiliates model so I can save new data in there.

The problem is it gives me a white page. It doesn’t tell me what the problem is… can someone please tell me what’s wrong? and every time I remove the $fav->save();, it starts working. So how does the save(); work? I need it to work to add new mysql record.

Help is very much apprectiated, thank you.

wondering the same thing. tell me if you know already ;D

Did you have a look at this http://www.yiiframework.com/doc/guide/1.1/en/database.ar Also can you expand your question, what are you trying to save. Next if you have any rules in your model the save method will validate and if what is being validated is not being passed then no insert will occur for a quick test try $fav->save(false); and see if insert will work. Let me know if it works.

how if I want to make a new form with the existing controller (for example: penjualan) and model. In this form, there are some blank textfield for qty which is will be saved in one column named qty in pembelian table (not penjualan) in db. plus, this qty is from different model (pembelian model), not the model of this controller. how to do the save action with this kind of situation? thanks in advance

Here is what I will do.




public function actionPenjualan()

    {

    $model = new Penjualan();

    $pembelian = new Pembelian();

    // Uncomment the following line if AJAX validation is needed

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

    if(isset($_POST['Penjualan']))

    {

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

    // save to pembelian

    $pembelian->qty = $model['qty']; // grab the qty value from Penjualan form

    $pembelian->save();

    //save other data to Penjualan

    if($model->save())

    // or you could save to pembelian after saving to Penjualan

    // save to pembelian

    //$pembelian->qty = $model['qty']; // grab the qty value from Penjualan form

    //$pembelian->save();

    // then redirect to whatever page

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

    }

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

    'model'=>$model,

    ));

    }



Let me know if it works I have done such cases in my other yii app where I save data to multiple tables with one controller. I have not tested

wow, thanks for the whole code. but you forgot 1 problem. ‘some qty’ not ‘1 qty’ to be inserted into 1 column named qty in different table instead of penjualan table. Which is means there are more than 1 qty that will be inserted. I’ve tried tabular input but it just makes me more confused hehe

use foreach.




public function actionPenjualan()

    {

    $model = new Penjualan();

    $pembelian = new Pembelian();

    // Uncomment the following line if AJAX validation is needed

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

    if(isset($_POST['Penjualan']))

    {

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

    // save to pembelian

    foreach($model['qty'] as $qty):

    $pembelian->qty = $qty; // grab the qty value from Penjualan form

    $pembelian->save();

    endforeach;

    //save other data to Penjualan

    if($model->save())

    // or you could save to pembelian after saving to Penjualan

    // save to pembelian

    //$pembelian->qty = $model['qty']; // grab the qty value from Penjualan form

    //$pembelian->save();

    // then redirect to whatever page

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

    }

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

    'model'=>$model,

    ));

    }



But if you are passing array of items like would update qty of items in a shopping cart check the following

check this extension too to manage tabular input http://www.yiiframework.com/extension/ztabularinputmanager

Also yii guide http://www.yiiframework.com/doc/guide/1.1/en/form.table