Create view in Yii2

Hello

In Yii2, the use of a model – extends yii\db\activRecord (table), a modeloSearsh (ActiveDataProvider) and view (GridView:: Widget), allows, in full view, filter by column.

I’d like to achieve the same using a query (yii\db\Query) rather than a table. The query will have the information from multiple tables but will have to be treated in query and not in table, and if I use the sqlDataProvider, column filters stop working no view (GridView:: Widget)

Can somebody help me?

I can’t apply

“Using SQL views for filtering, sorting and displaying data

There is also another approach that can be faster and more useful - SQL views. For example, if we need to show the gridview with users and their profiles, we can do so in this way:

CREATE OR REPLACE VIEW vw_user_info AS

SELECT user.*, user_profile.lastname, user_profile.firstname

FROM user, user_profile

WHERE user.id = user_profile.user_id

//****************

I saw the following explanation

“Using SQL views for filtering, sorting and displaying data

There is also another approach that can be faster and more useful - SQL views. For example, if we need to show the gridview with users and their profiles, we can do so in this way:

CREATE OR REPLACE VIEW vw_user_info AS

SELECT user.*, user_profile.lastname, user_profile.firstname


FROM user, user_profile


WHERE user.id = user_profile.user_id"

Then you need to create the ActiveRecord that will be representing this view:

//*

namespace app\models\views\grid;

use yii\db\ActiveRecord;

class UserView extends ActiveRecord

{

/**


 * @inheritdoc


 */


public static function tableName()


{


    return 'vw_user_info';


}





public static function primaryKey()


{


    return ['id'];


}





/**


 * @inheritdoc


 */


public function rules()


{


    return [


        // define here your rules


    ];


}





/**


 * @inheritdoc


 */


public static function attributeLabels()


{


    return [


        // define here your attribute labels


    ];


}

}

but I can’t apply

“Using SQL views for filtering, sorting and displaying data

There is also another approach that can be faster and more useful - SQL views. For example, if we need to show the gridview with users and their profiles, we can do so in this way:

CREATE OR REPLACE VIEW vw_user_info AS

SELECT user.*, user_profile.lastname, user_profile.firstname


FROM user, user_profile


WHERE user.id = user_profile.user_id

//**********************

//My Code is

//*****

//Controllers

use app\models\ObsinqSearch;

public function actionIndex()

{

$searchModel = new ObsinqSearch();


    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);





    return $this->render('index', [


	


	


	


        'searchModel' => $searchModel,


        'dataProvider' => $dataProvider,


    ]);

}

//**************************************************

//Models

//****************

//ModelSerarch

<?php

namespace app\models;

use Yii;

use yii\base\Model;

use yii\data\ActiveDataProvider;

//use yii\data\SqlDataProvider;

use app\models\Obsinq;

/**

  • ObsinqSearch represents the model behind the search form about app&#092;models&#092;Obsinq.

*/

class ObsinqSearch extends Obsinq

{

/**


 * @inheritdoc


 */


public function rules()


{


    return [


        [['id', 'teste1', 'teste2','submetidoObs'], 'safe'],


    ];


}





/**


 * @inheritdoc


 */


public function scenarios()


{


    // bypass scenarios() implementation in the parent class


    return Model::scenarios();


}





/**


 * Creates data provider instance with search query applied


 *


 * @param array &#036;params


 *


 * @return ActiveDataProvider


 */


public function search(&#036;params)


{


	


	 


//table in server but no working filters


//if delete table in server, no work bicause &quot;error table not exist&quot;


&#036;query = Obsinq::findbySql(&quot;


	SELECT 


	reg.id, 


	pop.teste1, 


	pop.teste2, 


	reg.teste1 obs


	from reg 


	LEFT JOIN (pop) 


	ON (pop.id = reg.id)


	where


	&#33;(reg.teste1 is null)


	and char_length(reg.test2) &gt; 1


	


	Order by reg.teste1&quot;);


	


	


&#036;dataProvider = new ActiveDataProvider([


	'query' =&gt; &#036;query,


    ]);


	


		





    &#036;this-&gt;load(&#036;params);





    if (&#33;&#036;this-&gt;validate()) {


    	return &#036;dataProvider;


    }





   


&#036;query-&gt;andFilterWhere([


    'id' =&gt; &#036;this-&gt;id,


        'teste1' =&gt; &#036;this-&gt;teste1,


    ]);





    &#036;query-&gt;andFilterWhere(['like', 'teste2', &#036;this-&gt;teste2])


        -&gt;andFilterWhere(['like', 'obs', &#036;this-&gt;obs]);





	





    return &#036;dataProvider;


}

}

//****************

//Model

<?php

namespace app\models;

use Yii;

use yii\db\ActiveRecord;

/**

  • This is the model class for table "vw_obsinq".

  • @property integer $id

  • @property integer $teste1

  • @property string $teste2

  • @property string $obs

*/

class Obsinq extends \yii\db\ActiveRecord

{

/**


* @inheritdoc


*/


public static function tableName()


{


return 'vw_obsinq';


}





public static function primaryKey()


{


return ['id'];


}








/**


 * @inheritdoc


 */


public function rules()


{


    return [


        [['id'], 'required'],


        [['id','teste1], 'integer'],


        [['teste2','obs'], 'string'],


    ];


}





/**


 * @inheritdoc


 */


public function attributeLabels()


{


    return [


        //'id' =&gt; 'ID',


        //'teste1' =&gt; 'teste1',


        //'teste2' =&gt; 'teste2',


        //'obs' =&gt; 'obs',


    ];


}

}

//******************************************************

//views

<?php

use yii\helpers\Html;

use yii\grid\GridView;

/* @var $this yii\web\View */

/* @var $searchModel app\models\ObsinqSearch */

/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = ‘Obsinqs’;

$this->params[‘breadcrumbs’][] = $this->title;

?>

<div class="obsinq-index">

&lt;h1&gt;&lt;?= Html::encode(&#036;this-&gt;title) ?&gt;&lt;/h1&gt;





&lt;p&gt;


    &lt;?= Html::a('Create Obsinq', ['create'], ['class' =&gt; 'btn btn-success']) ?&gt;


&lt;/p&gt;





&lt;?= GridView::widget([


    'dataProvider' =&gt; &#036;dataProvider,


    'filterModel' =&gt; &#036;searchModel,


    'columns' =&gt; [


       ['class' =&gt; 'yii&#092;grid&#092;SerialColumn'],





       [


      //o atribute permite visualizar o campo a procurar


      'attribute'=&gt;'id',


      //Nome do campo


      'label' =&gt; 'ID',


      //'format'=&gt;['decimal',0],


      'value' =&gt; 'id',


      'contentOptions' =&gt; ['align'=&gt; 'Left', 'width' =&gt; 120],


   ],


		





       [


      //o atribute permite visualizar o campo a procurar


      'attribute'=&gt;'id',


      //Nome do campo


      'label' =&gt; 'teste1',


      //'format'=&gt;['decimal',0],


          'value' =&gt; 'teste1',


          'contentOptions' =&gt; ['align'=&gt; 'Left', 'width' =&gt; 120],


       ],





       [


      //o atribute permite visualizar o campo a procurar


      'attribute'=&gt;'id',


      .//Nome do campo


      'label' =&gt; 'teste2',


      'value' =&gt; 'teste2',


      'contentOptions' =&gt; ['align'=&gt; 'Left', 'width' =&gt; 120],


   ],


       [


      //o atribute permite visualizar o campo a procurar


      'attribute'=&gt;'id',


      //Nome do campo


      'label' =&gt; 'obs',


      'value' =&gt; 'obs2',


      'contentOptions' =&gt; ['align'=&gt; 'Left', 'width' =&gt; 120],


   ],


       


       //['class' =&gt; 'yii&#092;grid&#092;ActionColumn'],


    ],


]); ?&gt;

</div>

Someone can help me please

Thanks

Why do you need in a single class? Relation between User and UserProfile should work well.

the need has to do with the way to obtain the data and server data hosting

I solved the problem by placing the conditions directly in query

//modelSearch

public function search($params){

$this->load($params);

$query = Obsinq::findbySql("

SELECT

reg.id,

pop.teste1,

pop.teste2,

reg.teste1 obs

from reg

LEFT JOIN (pop)

ON (pop.id = reg.id)

where

($this->id = ‘’ or

reg.id = ‘$this->id’) and

($this->teste1 = ‘’ or

pop.teste1 like ‘%$this->teste1%’) and

($this->teste2 = ‘’ or

pop.teste2 like ‘%$this->teste2%’) and

($this->obs = ‘’ or

reg.teste1 like ‘%$this->obs%’) and

!(reg.teste1 is null) and

char_length(reg.test2) > 1

Order by reg.teste1");

$dataProvider = new ActiveDataProvider([

‘query’ => $query,

]);

return $dataProvider;

}

I need to have a model(ActiveRecord) to an existing table (empty or not empty) on the server with the tutilizar fields in sql

The search fields are parameterized in the index (attribute) and not in the model