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
Page 1 of 1
Cmodel - Adding Attributes Dynamically
#1
Posted 05 March 2013 - 07:29 AM
Yii extension: Captcha Extended
Greatest discoveries in 22nd century will be about the gravitation. | Homepage: http://www.synet.sk
Greatest discoveries in 22nd century will be about the gravitation. | Homepage: http://www.synet.sk
#2
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;
}
#3
Posted 05 March 2013 - 08:25 AM
samdark, 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. | Homepage: http://www.synet.sk
Greatest discoveries in 22nd century will be about the gravitation. | Homepage: http://www.synet.sk
#4
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.
#5
Posted 06 March 2013 - 08:41 AM
samdark, 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. | Homepage: http://www.synet.sk
Greatest discoveries in 22nd century will be about the gravitation. | Homepage: http://www.synet.sk
Share this topic:
Page 1 of 1

Help
This topic is locked












