Yii Framework Forum: Access Old Attribute Values In Ar Model - Yii Framework Forum

Jump to content

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

Access Old Attribute Values In Ar Model Rate Topic: -----

#1 User is offline   neopersia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-January 12

Posted 19 April 2013 - 12:07 PM

Hi,

Is there any way to access the old attribute values after using the setAttributes method to assign new values?
I need to update a field if only the users has input some new values. in the other hand if the field is empty, save the old value (which is set as empty by not entering any values by the user).
0

#2 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 508
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 19 April 2013 - 12:43 PM

Check this extension. Even if you don't use it all you might benefit learning from the "dirtyness" check source code.
José
0

#3 User is offline   Ghanshyam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 18-March 13
  • Location:Ahmedabad

Posted 19 April 2013 - 12:43 PM

Hello,
You may do like...
in controller.
public function actionUpdateTable()
{
   $modelObj = DemoTable::model()->find('id='.$_GET['id']);
   if(isset($_POST['DemoTable']) && $_POST['DemoTable'])
   {
       $modelObj->firstname = $_POST['DemoTable']['firstname'];
       $modelObj->lastname = $_POST['DemoTable']['lastname'];


       if($_POST['DemoTable']['city']!=="")
       {
           $modelObj->city= $_POST['DemoTable']['city'];
       }
       
       $modelObj->save();
   }
}

0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,692
  • Joined: 04-March 10
  • Location:UK

Posted 19 April 2013 - 12:46 PM

I wrote a basic history logging behavior which does this:

	private $oldAttributes;
	
	public function afterFind($event)
	{
		$this->oldAttributes = $this->getOwner()->getAttributes();
	}


If you made $oldAttributes public or wrote an accessor for it, this could work for you. After creating the behavior, you'd just include it in the models which need the functionality like this:

	public function behaviors()
	{
		return array(
			'ActiveRecordHistoryLogger'
				=> 'application.models.behaviors.ActiveRecordHistoryLogger',
		);
	}


replacing the name and path as appropriate.
0

#5 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 19 April 2013 - 12:49 PM

Dear Friend

Declare a virtual property in the model.
public $oldRecord;


Overide the afterFind method of AR
public function afterFind()
    {
	$this->oldRecord=clone $this;
	parent::afterFind();		
    }


Now in controller in actionUpdate method, we can access the old values like below.
$model->oldRecord->password;
$model->oldRecord->name;


Regards.
1

#6 User is offline   neopersia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-January 12

Posted 19 April 2013 - 12:59 PM

Thank you all for replying.
In my case I think the way that seenivasan pointed out is the best option.

Cheers.
0

#7 User is offline   Robert McKenney 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-June 13

Posted 30 January 2014 - 07:24 PM

I know that this is a bit of an older post, but I wanted to offer a simpler solution that uses less memory.

The Yii AR models already store an array with the old values, so there is no need to clone them again. Simply access them like this:
$model->oldValues['attributeName']


For example in your User model you may want to make sure the password is hashed when updated.
public function beforeSave()
{
    if ($this->password != $this->oldValues['password']) { // password has changed, so we need to hash it.
        $this->password = CPasswordHelper::hashPassword($this->password,13);
    }

    return parent::beforeSave();
}

1

#8 User is offline   neopersia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 06-January 12

Posted 31 January 2014 - 02:01 AM

View PostRobert McKenney, on 30 January 2014 - 07:24 PM, said:

I know that this is a bit of an older post, but I wanted to offer a simpler solution that uses less memory.

The Yii AR models already store an array with the old values, so there is no need to clone them again. Simply access them like this:
$model->oldValues['attributeName']


For example in your User model you may want to make sure the password is hashed when updated.
public function beforeSave()
{
    if ($this->password != $this->oldValues['password']) { // password has changed, so we need to hash it.
        $this->password = CPasswordHelper::hashPassword($this->password,13);
    }

    return parent::beforeSave();
}



Thanks Robert,
It sounds good. I'll give it a try soon :)
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