Yii Framework Forum: $model->attributes - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

$model->attributes array copy from 1.0.11 to 1.1.0 Rate Topic: -----

#1 User is offline   smoothcoder 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 03-December 09
  • Location:Thailand

Posted 05 February 2010 - 08:56 AM

I used to have a user update from my blog model in 1.0.11 with :


public function actionUpdate()
     { $model=$this->loadUser();
         if(isset($_POST['User']))         
         {
 		$model->attributes=$_POST['User'];



This worked well when a user makes an update action (in 1.0.11). I Just tested it on DiscardedTeenz.com which still is on Yii framework 1.0.11.

However, I am testing a migration from the blog model to 1.1.0 with the just new dev.discardedteenz.com
(because I intend to release later this blog snippets and code when it's clean enough.)

>:( The problem is, in Yii framework yii-1.1.0.r1700. I can't update anymore with the above code.
I inserted :
var_dump($_POST['User']);echo "<br/>";
var_dump($model->attributes);echo "<br/>";


This is what I see:

Quote

array(9) { ["username"]=> string(4) "demo" ["password"]=> string(0) "" ["email"]=> string(14) "demo@gmail.com" ["url"]=> string(0) "" ["status"]=> string(1) "1" ["banned"]=> string(1) "0" ["avatar"]=> string(0) "" ["about"]=> string(16) "I am a demo user" ["newsletter"]=> string(1) "0" }

array(14) { ["id"]=> string(1) "2" ["username"]=> string(4) "demo" ["password"]=> string(0) "" ["email"]=> string(14) "demo@gmail.com" ["url"]=> string(0) "" ["status"]=> string(1) "1" ["banned"]=> string(1) "0" ["avatar"]=> string(0) "" ["newsletter"]=> string(1) "0" ["passwordLost"]=> NULL ["confirmRegistration"]=> NULL ["about"]=> string(15) "I wrote Moira ." ["created"]=> string(1) "0" ["lastlogin"]=> string(1) "0" }


The first array is $_POST['User']. I have the new about: I am a demo user
The second array is $model->attributes, after
$model->attributes=$_POST['User']


the array dimensions shows the affectation didn't happen well, and the about is the old one : I wrote Moira .


Is this broken in 1.1.0.r1700 ? Maybe I got wrong somewhere ??
How to bypass this cleanly ?
0

#2 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 11-August 09
  • Location:Argentina

Posted 05 February 2010 - 09:04 AM

attribute "about" is "safe"

In version 1.1, an attribute is considered safe if it appears in a validationrule that is applicable in the given scenario.

edit
http://www.yiiframew...ute-assignments

This post has been edited by Horacio Segura: 05 February 2010 - 09:05 AM

KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#3 User is offline   smoothcoder 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 03-December 09
  • Location:Thailand

Posted 05 February 2010 - 09:24 AM

Muchas gracias for the link.
Good to remind a few things.
In fact im my model I have :

	public function safeAttributes()
	{
		return array('username','password','status', 'banned','about','email',
		'url','password_repeat','verifyCode','usernameoremail','created','lastlogin','newsletter','_lang');
	}


May-be that's not enough ? ???
0

#4 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 11-August 09
  • Location:Argentina

Posted 05 February 2010 - 09:29 AM

View Postsmoothcoder, on 05 February 2010 - 09:24 AM, said:

Muchas gracias for the link.
Good to remind a few things.
In fact im my model I have :

	public function safeAttributes()
	{
		return array('username','password','status', 'banned','about','email',
		'url','password_repeat','verifyCode','usernameoremail','created','lastlogin','newsletter','_lang');
	}


May-be that's not enough ? ???



nop

put

public function rules()
    {
        return array(
...
            array('about','safe'),
....

        );
    }

KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#5 User is offline   smoothcoder 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 03-December 09
  • Location:Thailand

Posted 05 February 2010 - 09:43 AM

YYEEESSSS !

It works.

From your link :
In version 1.1, an attribute is considered safe if it appears in a validation rule that is applicable in the given scenario. For example,


That's true. The other attributes from the user were mentioned in rules except for my "about". So it worked when I added
array('about','safe'),



Reading :

Quote

The policy of deciding which attributes are safe is different in version 1.0 and 1.1. In the following, we will describe them separately.


I am wondering if the public function safeAttributes() is still of any use in 1.1 ?
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 05 February 2010 - 09:51 AM

Quote

I am wondering if the public function safeAttributes() is still of any use in 1.1 ?


There is no safeAttributes() method in 1.1. anymore. There is only safeAttributesNames(), which returns names of attributes you declared "safe" in rules().
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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