在我的应用程序中,我使用了一个扩展(我拥有并且可以修改)。这个扩展有一个基于 ActiveRecord 的类,我想用另一个属性在应用程序中扩展它。我能以某种方式做到这一点吗?工厂可以帮助任何方式或Yii 行为吗?
扩展类:
namespace extension;
/**
* @property integer $id
* @property string $name
*/
class Product extends ActiveRecord {
public static function tableName() {
return 'product';
}
/**
* @inheritdoc
*/
public function rules() {
return [
[['id', 'name'], 'required'],
[['id'], 'integer'],
[['name'], 'string'],
];
}
}
扩展中还有一个 ProductController 和相应的视图文件(索引、创建、更新、视图、_form),它们是用 gii 定期生成的。我只想向$description
产品添加另一个属性(字符串,必需)。可以进行迁移以添加所需的列。
我是否必须覆盖模型和控制器类以及视图文件?还是更优雅的解决方案?
例如,考虑在扩展中发生的标准对象创建:
public function actionCreate() {
$model = new Product();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'language' => $model->language]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
根据我的理解,我无法影响创作。还是我错了?
我的印象是我必须覆盖所有内容(还有视图文件,因为必须显示属性),然后更改controllerNamespace。