Set some active record as read only

Is it possible?

I have used this solution: http://www.yiiframework.com/doc/cookbook/14/

So in my model:




array('DataAtualizado', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => false, 'on' => 'update'),


array('DataInserido, DataAtualizado', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => false, 'on' => 'insert'),



It works ok.

The problem is.

When I find() some model, and update some fields and later save() it, i got this error:

[color="#FF0000"]General error: 1292 Incorrect datetime value: ‘17/09/2009 14:04:26’ for column ‘DataInserido’ at row 1[/color]

This occurs because i format my date in afterFind().

One solution for this is to check isNewRecord() inside de beforeSave().

But would be nicer if I can tell to activerecord this: "This field is read only, can be set only the creation not in update. So, ignore this field in update statement".

I wish i was clear! :D

thanks

Im not really sure about it but i think you can do that in the SafeAttributes Method like this:


public function safeAttributes()

	{

	    return array('attr1','attr2', 

	    	    'create' => 'DataInserido, DataAtualizado, attr1, attr2',

	    	    'update' => 'DataAtualizado, attr1, attr2',

	);

}

Not tested though.

Thanks for the help.

But unfortunately this method only works for "mass assignment".

I had to solve it inside the beforeSave() callback. <_<

And what exaclty do you do? I may need to use this someday ::)

I didn’t like the solution, but this works.




public function beforeSave()

{

   if (isset($this->DataInserido) && !$this->isNewRecord)

       $this->DataInserido = toMySqlDateTime($this->DataInserido);

}



Well ive been looking at this and it looks like its not within Yiis functionality, but it looks like you can create a Validator for this. looks like the best aproach to me, I also though about maybe creating a Behavior but it looks smooter if you can add this kind of things to the rules() method in the model:

You may like it, something like:




public function rules()

	{

		return array(

			array('DataInserido, DataAtualizado', 'contraints', 'readOnly'=>true),

		);

	}



And for your exact case:




public function rules()

	{

		return array(

			array('DataInserido, DataAtualizado', 'contraints', 'readOnly'=>true, 'on'=>'update'),

		);

	}



And create a validator extending CValidator.

just an idea.

I was reading the documentation and found the saveAttributes() method from CActiveRecord.

It works perfectly, but… write all the thousand fields is boring and can make some bugs in feature because of forgetness.

It could solve the problem with something like this:


saveAttributesExcept($attri, $listOfDontSaveParams);

I think it’s easier.

Well I like the idea of a validator as you can not only use it for that but extend it for some other validatios alike, you can also use the validator for all other models and you wont need to write a


saveAttributesExcept($attri, $listOfDontSaveParams);

every time you need a model validated like that. you just add the rule among your uther rules and thats it.

Yeah, well… validator ideia seems to be a good solution for this problem.

I forgot to thank you. :unsure:

hi frnds,

can u please tell me hw to create a new validator??

i’m new to yii…