Extend Yii2 Controller And Activerecord

Hi All,

I’m trying to upgrade from 1.1 to yii2 but i was extending the base controller and model on the old code, can any one advice us what’s the right way to do this in Yii2.

My current idea is to have my own namespace package which extend yii2 core classes with the same structure like

mynamespace/base/Controller

mynamespace/web/Response

and then using it on my controllers rather than yii ones like the following

use mynamespace\web\Controller; //NOT use yii\web\Controller;

so this my idea.

I have another question about the database relations, how i can do a query on table with three other tables in the relation , for example in yii 1.1i was able to say

$rows=$this->with(‘model1’,‘model2’,‘model3’)->findAll(‘condition’=>‘model1.field=x AND model2.field=Y’));

how i can do the same on Yii2?

There is no need to use the same namespace you can simply put your base controller in app\components\Controller like it was in yii1.1

You have to manually do the join for model 1,2 and 3 because related models are loaded after the find query.




$rows=MyModel::find()

    ->join('JOIN', 'tbl_model1', 'ON condition goes here')

    ->join('JOIN', 'tbl_model2', 'ON condition goes here')

    ->join('JOIN', 'tbl_model3', 'ON condition goes here')

    ->with('model1','model2','model3')

    ->where('tbl_model1.field=x AND tbl_model2.field=Y')

    ->all();



That’s sound good but i tried and i didn’t worked , what i tried exactly is just copying my controller to frontend/components/Controller.php but it’s still not consider it, did i need to define something in the configuration file or change namespace to something like ‘use app\components\Controller;’

Of course! Migrating from yii 1.1 to 2.0 is not an easy job. Yii 2 is a complete rewrite and many things have changed. You have to make your classes namespaced and adjust everything to the new implementations. Many parts of your code also may need to be rewritten completely.

check the guide on differences to 1.1: https://github.com/yiisoft/yii2/blob/master/docs/guide/upgrade-from-v1.md

Also you might first play around with a yii2 example application to see how it works before trying to migrate an existing app.

I can’t find that logical if I’m gonna to define the join condition every time i want to do like this query,there is now way or option in ‘with’ to make it do the join automatically, for example like this:


$rows=MyModel::find()

    ->with('model1','model2','model3')

    ->join(true)

    ->where('tbl_model1.field=x AND tbl_model2.field=Y')

    ->all();

or replace ‘with’ by ‘join’ without need to define the condition


$rows=MyModel::find()

    ->join('model1','model2','model3')

    ->where('tbl_model1.field=x AND tbl_model2.field=Y')

    ->all();

or may be even we can do this


$rows=MyModel::find()

    ->with('model1'=>array('join'=>true),'model2'->array('join'=>true),'model3'=>array('join'=>true))

    ->where('tbl_model1.field=x AND tbl_model2.field=Y')

    ->all();

Please read the available docs to understand usage of database objects for Yii2. There are many changes, and many new features in Yii 2 vs Yii 1.

You may want to go through Yii 2 Guides for Query Builder and Active Record once again. All your join conditions mentioned can be achieved (including more options).

@Devypt,

at the end, wjere did you place your own controllers ? under which folder ?

Simple but excellent post about that: http://www.jamesbarnsley.com/site/2016/03/14/create-a-master-model-and-controller-in-yii2/

and here another one:

http://stackoverflow.com/questions/27180059/execute-my-code-before-any-action-of-any-controller