How to save 2 datetime in 2 column using AR

Hi All,

I have a problem saving 2 datetime value in 2 different column by using AR method. Below was my script:

Controller :

<code>

$tokenData = array(

            'customers_id' =&gt; &#036;customerId,


            'customers_setting_key' =&gt; 'security_token_request',


            'customers_setting_value' =&gt; '1',


				'created_datetime2' =&gt; date(&quot;Y-m-d H:i:s&quot;),


            'updated_datetime' =&gt; date(&quot;Y-m-d H:i:s&quot;)


            );

$modelCustomersSetting->saveCustomerSetting($tokenData);

</code>

model (CustomersSetting):

<code>

public function saveCustomerOpt($customerOptData){

&#036;this-&gt;setIsNewRecord(true);


&#036;this-&gt;attributes = &#036;customerOptData;


&#036;this-&gt;save();

}

</code>

can some help me?

Hi hoonglc,

Just for clarification, what exactly is your problem?

Does one of them not saved? Or do they have different times?

Hi,

I actually save but one of the datetime file is not save, become default 0000-00-00 00:00:00, i have confirm i put the correct info but it still same, i have google for this problem and finally i found one method is work but i not sure is it the correct way to do it or not.

The solution was add both field into the rules, set to safe.

The validation of the model occurs when you call save().

And if the validation rules for created_datetime2 and/or updated_datetime are not properly setup, then the values may not be set correctly.

If you are sure that user input doesn’t come into those datetime fields, then you can set them as ‘safe’ … what are the original rules for them, BTW?

Alternatively, you may set ‘runValidation’ parameter of save() to false to skip the validation.

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail

Hi,

The original rules was just have only datetime_1 is in safe array, datetime_2 was not in the rules. The datetime input was safe because it is set by own script. Anyway, thanks for your information. I will take a look for the link, hope can have a proper solution for this issue.