- There is model
class Country extends ActiveRecord{
public static function tableName()
{
return 'countries';
}
}
- Trying to extend ActiveRecord with method getDataProvider()
class ActiveRecordEx extends ActiveRecord {
public function getDataProvider($pageSize=20){
$options = ['query' => static::find()->asArray()];
if($pageSize)
$options['pagination']= ['pageSize' => $pageSize];
return new ActiveDataProvider($options);
}
}
and use it
class Country extends ActiveRecordEx{…}
All works fine:
$dictionary = new Country();
$dataProvider = $dictionary->getdataProvider(); //is usable for data widgets
$model = $dictionary->findOne(['id'=>$recId]); // $model has all db table attributes
- But when some of public Country attributes is defined it becomes inaccessible in model.
For example, after declaring id:
class Country extends ActiveRecordEx{
public $id;
public static function tableName() {…}
}
$model = $dictionary->findOne(['id'=>$recId]); // $model->id is null
Debugger shows that all attributes (include id) have correct values in yii\db\BaseActiveRecord->_attributes.
Environment is Yii 2.0.3 on PHP 5.5.23.
What is correct way to use model attributes?