March 20, 2011, 5:37pm
Why when you insert a yii CActiveRecord fill in all fields?
There is table in database:
ORDERS (ID, FIO, STATE, CLIENT_TYPE).
Default database fields values are STATE =‘unplanned’, CLIENT_TYPE = ‘client’;
$model = new Order;
$model->FIO = 'J. Smith';
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:
March 21, 2011, 6:37am
March 21, 2011, 8:57am
You should clean default values.
After create the object, use:
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.
March 21, 2011, 10:02am
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).
CREATE TABLE "SCHEMA"."TEST" (
“STOP_DATE” DATE DEFAULT TO_DATE(‘01.01.9999’,‘dd.mm.yyyy’)
Until unset attributes model don’t saving.
March 21, 2011, 10:38am
That looks like a bug of the db connection for oracle. Unfortunalety I have no experience and cannot help you more.