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’));
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.
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).