ActiveRecord inser default_data fields

Why when you insert a yii CActiveRecord fill in all fields?

Example:

There is table in database:

ORDERS (ID, FIO, STATE, CLIENT_TYPE).

Default database fields values are STATE =‘unplanned’, CLIENT_TYPE = ‘client’;




function actionInsert()

{

$model = new Order;

$model->FIO = 'J. Smith';

$model->save()

}

CActiveRecord generated SQL:


INSERT INTO "ORDER" ("FIO","STATE", "CLIENT_TYPE")

VALUES (:yp0, :yp1, :yp2) RETURNING "ID" INTO :RETURN_ID

But in my opinion is better this SQL:


INSERT INTO "ORDER" ("FIO")

VALUES (:yp0) RETURNING "ID" INTO :RETURN_ID

a missed field to leave the database - this will correct for the database.

Indication of the fields in the save() method I know.

How to do it by default?

I think you’re leaning towards saveAttributes(), you can basically choose which attributes you want to be set but bear in mind this attributes will will not validated. Read: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#saveAttributes-detail

Jaymard

Issue for this problem:

http://code.google.com/p/yii/issues/detail?id=2219

You should clean default values.

After create the object, use:


$model->unsetAttributes();

There has already been a discussion if load by default the default value or not. In that moment (some month ago) the project leaders choose to load default value, and if thery are not needed they should unset.

The other way is not to load as default and create a function for load if needed, that is what you propose.

The solution are both reasonable, but the development team choose the first one, so you have to use unsetAttributes, even if it looks strange, because I think that they will not change their decision as it would break bc.

Thanks, zaccaria.

Method unsetAttributes() is really help me.

But the problem with default_data in oracle tables exists, i am waiting for its solution (issue link above).

Example:

CREATE TABLE "SCHEMA"."TEST" (

“STOP_DATE” DATE DEFAULT TO_DATE(‘01.01.9999’,‘dd.mm.yyyy’)

)

Until unset attributes model don’t saving.

That looks like a bug of the db connection for oracle. Unfortunalety I have no experience and cannot help you more.