Yii Framework Forum: Cmodel - Adding Attributes Dynamically - Yii Framework Forum

Jump to content

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

Cmodel - Adding Attributes Dynamically

#1 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 05 March 2013 - 07:29 AM

Hi,

I am just curious - will it be possible to add new attributes into CModel (or whatever equivalent in Yii2) dynamically, during the run-time?

In some situations, we dont exactly know the structure of the CModel.
The only ways is to override CComponent::__set method, which is not nice.

Currently this is one of few missing features I guess:-)

Thank you.
Lubos
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
0

#2 User is offline   samdark 

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

Posted 05 March 2013 - 07:32 AM

Yes, you will be able to do so but you'll need to override default attributes method that's returning public and non-static properties currently:

/**
 * Returns the list of attribute names.
 * By default, this method returns all public non-static properties of the class.
 * You may override this method to change the default behavior.
 * @return array list of attribute names.
 */
public function attributes()
{
	$class = new \ReflectionClass($this);
	$names = array();
	foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
		$name = $property->getName();
		if (!$property->isStatic()) {
			$names[] = $name;
		}
	}
	return $names;
}

Yii 1.1 Application Development Cookbook

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

#3 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 05 March 2013 - 08:25 AM

View Postsamdark, on 05 March 2013 - 07:32 AM, said:

Yes, you will be able to do so but you'll need to override default attributes method that's returning public and non-static properties currently:

/**
 * Returns the list of attribute names.
 * By default, this method returns all public non-static properties of the class.
 * You may override this method to change the default behavior.
 * @return array list of attribute names.
 */
public function attributes()
{
	$class = new \ReflectionClass($this);
	$names = array();
	foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
		$name = $property->getName();
		if (!$property->isStatic()) {
			$names[] = $name;
		}
	}
	return $names;
}



Ah, interesting to see fragment from Yii 2:-)

However, the method above is a getter. I was asking about a setter - setting some attribute that during rendering would be attached (injected) into CModel.

Thanx a lot.
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
0

#4 User is offline   samdark 

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

Posted 05 March 2013 - 12:03 PM

Setter relies on what is returned from attributes so it's possibe to achieve what you need. So you'll end up with something like FlexibleModel extends Model {} that you'll use as a base for such models.
Yii 1.1 Application Development Cookbook

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

#5 User is offline   lubosdz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 115
  • Joined: 25-July 10
  • Location:Slovakia, Bratislava

Posted 06 March 2013 - 08:41 AM

View Postsamdark, on 05 March 2013 - 12:03 PM, said:

Setter relies on what is returned from attributes so it's possibe to achieve what you need. So you'll end up with something like FlexibleModel extends Model {} that you'll use as a base for such models.


OK, thank you - i will wait to see Yii2 CComponent magic methods (__set, __get).
Yii extension: Captcha Extended

Greatest discoveries in 22nd century will be about the gravitation. | http://www.synet.sk | http://ipdf.sk
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