Yii Framework Forum: Make massive assignment work with setters in CActiveRecords too - Yii Framework Forum

Jump to content

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

Make massive assignment work with setters in CActiveRecords too So far it only works with CFormModel Rate Topic: -----

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 August 2012 - 08:29 AM

If you have an ActiveRecord with custom setters like this:

<?php
class MyRecord extends CActiveRecord
{
    ...
    public function setTest($value)
    {
        $this->something = $value;
    }
}


then you can't assign test with massive assignment:

<?php
$model = new MyRecord;
$model->setAttributes(array(
    'test' => 'testvalue',
));



because in CActiveRecord::setAttributes() we use property_exists(), which does not detect getters/setters:

<?php

    public function setAttribute($name,$value)
    {
        if(property_exists($this,$name))
            $this->$name=$value;
        else if(isset($this->getMetaData()->columns[$name]))
            $this->_attributes[$name]=$value;
        else
            return false;
        return true;
    }



This is an unneccesary limitation i think. It also works for CFormModel so why not make it work for ActiveRecords, too?

Use cases:

- Deal with HAS_MANY relations, e.g. create/delete entries in the connection table inside the setter
- "Virtual attributes" for formatting/parsing decimals/dates (i18n)
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,425
  • Joined: 17-January 09
  • Location:Russia

Posted 03 August 2012 - 03:00 PM

Makes sense. Post it to github. Ideally with pull-request.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   Mylek 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 22-February 12

Posted 06 November 2012 - 07:40 AM

I needed this in something I was working on so I made the change below in case anyone else wants it. Code might be off a bit but seems to be working for me so far.

	public function setAttribute($name,$value)
	{
		if(property_exists($this,$name))
			$this->$name=$value;
		else if(isset($this->getMetaData()->columns[$name]))
			$this->_attributes[$name]=$value;
	        else if(method_exists($this,'set'.ucfirst($name)) && $name != 'attributes' && ucfirst($name) != 'Attribute') {
	            $this->{'set'.ucfirst($name)}($value);
        }
		else
			return false;
		return true;
	}

2

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