2

I have an ActiveRecord class where I need to have some kind of dynamic variables in it.

The dynamic variable should be created for each existing variable. For example if I have a variable $test in my class, then it should also has $testDyn.

Example:

class User extends ActiveRecord
{
    public $name;
    public $age;
    public $address;

    /**
     * dynamic vars
     */
    public $nameDyn;
    public $ageDyn;
    public $addressDyn;

    public function tableName()
    {
        return 'user';
    }
    ...
}

There are ActiveRecords which have many variables and it would not be the best idea to do and manually create those dynamic vars for each variables. Are there any easy way / behavior which can do this?

1 Answer 1

3

You can create custom behavior for these purposes and attach for every needed model.

As for properties, you can explicitly specify it via parameter through behavior config, or use something like this:

$reflect = new ReflectionClass(new YourClass());
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);

More info and examples are available here.

You can additionally use some naming convention.

For dynamic properties you can override __get() magic method in behavior. See here.

Yii2 example (taken from yii2tech/ar-linkmany behavior by Paul Klimov, one of the framework contributors):

/**
 * PHP getter magic method.
 * This method is overridden so that relation attribute can be accessed like property.
 *
 * @param string $name property name
 * @throws UnknownPropertyException if the property is not defined
 * @return mixed property value
 */
public function __get($name)
{
    try {
        return parent::__get($name);
    } catch (UnknownPropertyException $exception) {
        if ($name === $this->relationReferenceAttribute) {
            return $this->getRelationReferenceAttributeValue();
        }
        throw $exception;
    }
}

To understand more about behaviors concept, please refer to according official docs section Behaviors.

Sign up to request clarification or add additional context in comments.

4 Comments

the problem is when i'm doing a find() on an active record, there are lots of columns which is being added to the select statement (e.g. MAX(price) as maxPrice). The sql will return the maxPrice but i will not be able to access it in my activerecord unless i have a public $maxPrice. And there are many of them. Would you have any idea how to implement any behabior to do this?
This is normal behavior and described here. If you provide more details, maybe we can advice more. I answered according to current question formulation.
The link which you added in your comment is `Selecting extra fields' which would require me to declare every single attribute manually. There is no behavior, which i can attach, on that page. I need to do the same thing but for many attributes, thus a behavior would really help to dynamically create those attributes.
You can try to simplify it with help of behavior, and I can't remember any existing solutions for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.