2017-09-25 93 views
0

嘗試將我的CRM系統的屬性添加到我的AR模型中。yii2在運行時添加屬性

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; 
} 

這裏是結果:

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

等待最佳的解決方案來解決這個問題...

回答

0

首先定義模型中的所有屬性,當你創建一個模型GII你將會看到屬性定義。

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 
*/ 
相關問題