0

trying to add property to my AR model dinamically for my CRM system.

public function getCustomFields($key = null)
{
    if (static::$customFields === null)
    {
        $fields = (new Query)->from('client_fields')->indexBy('key')->all(static::$conn);

        if ($columns = static::$conn->getSchema()->getTableSchema('client_profiles')->getColumnNames())
        {
            foreach ($columns as $column)
            {
                if (isset($fields[$column]))
                {
                    static::$customFields[$column]['name'] = $fields[$column]['name'];
                    static::$customFields[$column]['settings'] = @json_decode($fields[$column]['settings'], true);
                }

                if( ! isset($this->{$column}))
                {
                    $this->{$column} = null;
                }
            }
        }

        static::$customFields = static::$customFields ?: [];
    }

    return $key ? (isset(static::$customFields[$key]) ? static::$customFields[$key] : null) : static::$customFields;
}

And here is the result:

Setting unknown property: common\modules\crm\models\CrmClient::user_id

Waiting for best solution to resolve this problem...

1 Answer 1

1

Define all property in your model first, When you create a model Gii you will see the property definition.

e.g

/**
 * This is the model class for table "user_activity".
 *
 * @property integer $ua_id
 * @property integer $user_id
 * @property string $filename
 * @property integer $created_at
 *
 * @property User $user
 */
Sign up to request clarification or add additional context in comments.

Comments

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.