[color="#FF0000"]"Argument 1 passed to common\\models\\Page::__construct() must be an instance of common\\components\\Translator, none given, called in /var/www/html/gtbcloud/vendor/yiisoft/yii2/db/BaseActiveRecord.php on line 1163"[/color]
Question:
[size="5"][color="#0000FF"]How do I automaticaly inject my dependencies to my constructor using DI???[/color][/size]
public function actionUpdate()
{
$page = Page::findOne(\Yii::$app->request->get('id'));
$page->attributes = \Yii::$app->request->post();
$page->save();
return $page->getResponse();
}
I have a question…
In my rest controller, I can now just delete my custom actionUpdate() and the yii\rest\ActiveController will [color="#FF8C00"]handle the Update[/color].
[color="#FF0000"]But not the Create.[/color]
I can see the yii\rest\CreateAction creates a new model like this:
But I fall again in the "[color="#000080"]new[/color]" trap. ActiveDataProvider use "[color="#000080"]new[/color]" in setSort()
yii\data\ActiveDataProvider
...
public function setSort($value)
{
parent::setSort($value);
if (($sort = $this->getSort()) !== false && $this->query instanceof ActiveQueryInterface) {
/* @var $model Model */
$model = new $this->query->modelClass; //<--- here
...
It makes me rethink my entire approach. Why Yii2 use "[color="#000080"]new[/color]" everywhere while pushing their "Dependency Injection Container" feature that is not compatible with "new"?
That means the only way I can interact with my model is through my own custom code … I can’t even use a basic feature like “ActiveDataProvider”.
[size="5"][color="#0000FF"]How can I setup my model to use DI and still be compatible with "new"??
Is there something I’m missing? …I just don’t get it [/color][/size]