AR->validate() returns "No columns are being updated...""

Hi Folks,

I can’t get my AR model validated right. Or maybe this Exception is right and i assume wrong.

I always get Exception: "No columns are being updated for table xy".

but i’m don’t update just validate




// working with activeRecords

$model = new Xy();

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

// all attributes are set correct

// a table row already exists with same Pk set


// I even do

$model->setIsNewRecord(false);

$model->setScenario('update');


$model->validate();

// Throws Exception: updateCDbException ... No columns are being updated for table model

// I expect it to validate cos all attributes are ok



Thx for Help

I think that AR is designed to work in the following 2 different modes.

[list=1]

[*]create a new model instance … stuff it with contents … save (creating)

[*]load a model instance from db … edit its contents … save (updating)

[/list]

And what you are doing is …

  • create a new model instance … stuff it with contents … overwrite(update) the existing db record by saving

If you want to update an existing db record, you’d be better start with loading the model instance from db, not with creating it by “new Xy()”.

Well, thanks for that. I always wondered why all data in actionUpdate is loaded first…

But now that this is in the right place I don’t really understand how to set new data do the related model.

i$_POST Data comes as an array for related models.

so how can i tell which related model[] to assign which _POST[][]

Yes, I read the Yii Guide but somehow i can’t get it.

Scenario

In actionUpdate I (now) load my model data with related models and want to assign my form data to the related models.

Related model comes as an array in POST[‘Offercontents’][] cos it’s a repeating form field.




public function actionUpdate(){

// load model and data from db

// related models are defined well in relations and stuffed with data

$model = Offer::model()->with('offercategories', 'offercontents', 'offerimages', 'offerlocations')->findByPk($id);


// its clear to manipulate $model itself

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


// but what about eg. offercontents? which is an array

// how to find the the right $model->offercontents[] to manipulate?

$model->offercontents[?]->attributes = $_POST['Offercontents'][?];


// i really can't see that $_POST['Offercontents'][?]

}



Sorry for being so complicated. but i can’t even express well what the problem is. But you get it.

Thanx

OK, I think I’ve got your situation.

Take a look at the following section of the guide.

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

It will tell you how to handle your Offercontents models in an array.

Then you can combine the logic for Offercontents into actionUpdate for Offer.

[EDIT]

There can be a situation that you want to add one or more new Offercontens to an existing Offer.

In that case what you have to do is updating Offer(if necessary) and creating new Offercontents, not updating them.

With your advice finally got it working with tree levels of nested Forms.

What a monster.

Appreciate