Yii Framework Forum: Set some active record as read only - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Set some active record as read only Rate Topic: -----

#1 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 23 September 2009 - 09:49 AM

Is it possible?

I have used this solution: http://www.yiiframew...oc/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:

General error: 1292 Incorrect datetime value: '17/09/2009 14:04:26' for column 'DataInserido' at row 1

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
0

#2 User is offline   Asgaroth 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 371
  • Joined: 17-July 09
  • Location:Bogota, Colombia

Posted 23 September 2009 - 10:26 AM

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.
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.



- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
0

#3 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 23 September 2009 - 11:40 AM

Thanks for the help.

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

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

#4 User is offline   Asgaroth 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 371
  • Joined: 17-July 09
  • Location:Bogota, Colombia

Posted 23 September 2009 - 11:43 AM

And what exaclty do you do? I may need to use this someday ::)
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.



- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
0

#5 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 23 September 2009 - 11:46 AM

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

public function beforeSave()
{
   if (isset($this->DataInserido) && !$this->isNewRecord)
       $this->DataInserido = toMySqlDateTime($this->DataInserido);
}

0

#6 User is offline   Asgaroth 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 371
  • Joined: 17-July 09
  • Location:Bogota, Colombia

Posted 23 September 2009 - 12:27 PM

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.
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.



- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
0

#7 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 24 September 2009 - 09:18 AM

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.
0

#8 User is offline   Asgaroth 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 371
  • Joined: 17-July 09
  • Location:Bogota, Colombia

Posted 24 September 2009 - 10:12 AM

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.
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.



- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
0

#9 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 24 September 2009 - 11:28 AM

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

I forgot to thank you. :unsure:
0

#10 User is offline   vishnu prashanth 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-August 12

Posted 11 September 2012 - 05:28 AM

hi frnds,
can u please tell me hw to create a new validator??
i'm new to yii...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users