0

In my application I'm using an extension (which I own and may modify). This extension has an ActiveRecord based class that I want to extend in the application with another property. Can I do this somehow? Can a Factory help anyhow or Yii behaviour?

Class in extension:

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'],
        ];
    }
}

There is also a ProductController and the corresponding view files (index, create, update, view, _form) in the extension which were regularly produced with gii. I just would like to add another property $description (string, required) to the Product. A migration in order to add the required column is available.

Do I have to overwrite the model and controller class and the view files? Or is the a more elegant solution?

E.g., consider the standard object creation that takes place within the extension:

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,
        ]);
    }
}

From my understanding I cannot influence the creation. Or am I wrong?

My impression is that I have to override everything (also the view files since the property has to be displayed) and then change controllerNamespace.

robsch
  • 9,358
  • 9
  • 63
  • 104
  • You could simply make your extension configurable... Or you could use class map feature : http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html#class-map – soju May 17 '17 at 15:23
  • @soju Class map seems to me a bit hacky. Isn't it? In which way could I make the extension configurable (I'm still not that familiar with the whole configuration thing)? It would be okay to prepare the extension for that. – robsch May 17 '17 at 15:31

1 Answers1

0

As far as extending a class, just go ahead and add the property. Your new class will have inherited everything from the parent class plus have your new property.

Concerning overwriting the generated files, there is a diff option in gii when you generate the files, so you can preview and decide what to keep and what to overwrite. Be aware that you have to merge any changes manually however.

dataskills
  • 646
  • 7
  • 15
  • I know how to extend classes. Think about where the object gets created: in the extension. How should I change the class that gets used when the object is created? I'd like to use the rest (controller, view, other logic) of the code in the extension. – robsch May 17 '17 at 15:12
  • I'm sorry I misunderstood you, I may still misunderstand, but if I do understand you correctly: I think you just simply have to determine which object you want to create in the controller and account for the extra property. Or, perhaps you could always use the "new" version of the class since it is a superset of the old. It should generally work in all cases the old one did. – dataskills May 17 '17 at 19:18