Active Record With Dataproviders

Hi

I am using this code, which fails:




	public function getProducts($id)

	{


		$model = new \yii\data\ActiveDataProvider([

			'query' => Product::find()->where(['category_id' => $id])->all(),

		    'pagination' => [

          		'pageSize' => 5,

     		]  	

     	]);


		return $model;

	}



But I notice from the docs that if I change the ‘query’ to just Product::find(), it will work fine.

Why is that and what is the correct way to use Active record + where clauses with a DataProvider? ::)

What is the error you are getting?

Can you debug separately - what is the result of the following query for your scenario:




$id = xyz; // the id for your category

$query = Product::find()->where(['category_id' => $id])->all();



Hi Kartik V

Sorry I forgot to put my error in:

[yii\base\InvalidConfigException] exception ‘yii\base\InvalidConfigException’ with message 'The “query” property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.

The query worked fine when it wasn’t in the form of a DataProvider. I removed the all() from the query and it works now, but I am curious as to why it doesn’t work with the all() included?

Yes, you need to pass the QueryInterface object to the ActiveDataProvider (refer here) and not the output of the query.

Appending all() will return the array of results of the executed query.

stored procedure---------

ALTER PROCEDURE [dbo].[Emp1]

AS

BEGIN

-


SET NOCOUNT ON;

DECLARE @count int;

DECLARE @job_id varchar(max);

SET @count=0;

select a.emp_id,a.empcode,a.employee_name,a.email,a.dateofjoining,a.martial_status,a.gender,b.designation_name,a.email,a.ctc,a.pan_no,a.aadhar,a.passportno,a.passportexpno,a.bank_account_holder_name,a.dateofbirth,

a.father_name,a.uan_no,a.spl_pay,a.annual_salary,a.salary_structure,d.salary_structure,d.salary_structure_id,k.username,a.current_location,a.pf_no,a.bank_branch,a.bank_account_no,e.branch_name,f.emp_type,a.mobile,a.gender,a.created_by,a.modified_by,a.created_date,a.modified_date,a.pf_status,a.pt_status,c.institute_name_short,g.shift_start_time,g.shift_end_time,g.shift_name,h.employee_name as leave_approver1,i.employee_name as leave_approver2,j.employee_name as reporter_name

from acerp_employee_details a inner join acerp_designation b on a.designation=b.designation_id inner join acerp_institute c on c.institute_id=a.institute_id inner join acerp_salary_structure d on a.salary_structure=d.salary_structure_id inner join acerp_branch_type e on a.branch_id=e.branch_id

inner join acerp_employee_type f on a.emp_type_id=f.emp_type_id inner join acerp_shift_table g on g.shift_category_id=a.shift_category_id inner join acerp_employee_details h on h.emp_id=a.leave_approver1 inner join acerp_employee_details i on i.emp_id=a.leave_approver2 inner join acerp_employee_details j on j.emp_id=a.report_id inner join acerp_user k on k.id=a.emp_id where a.empcode is not null

END

-----------------------model----------------

public function empmaster($params){

    $command = Yii::$app->db->createCommand('Emp1');


$query = $command->queryAll();


 $dataProvider = new ActiveDataProvider([


        'query' => $query,


   ]);





    $this->load($params);





   if (!$this->validate()) {


       // uncomment the following line if you do not want to any records when validation fails


        // $query->where('0=1');


       return $dataProvider;


    }


    return $dataProvider;


   


}

---------------------view--------------------

<?= GridView::widget([

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


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


     'containerOptions'=&gt;['style'=&gt;'overflow: auto'],


'tableOptions' =&gt; ['perfectScrollbar'=&gt;true],


 'floatHeader'=&gt;true,


'panel' =&gt; [





    'type' =&gt; 'primary',


    'heading' =&gt; &quot;&lt;b&gt;&lt;center&gt;Employee Master&lt;/center&gt;&lt;/b&gt;&quot;,


],


    'beforeHeader'=&gt;[


    [


    'columns'=&gt;[


    ['content'=&gt;'', 'options'=&gt;['colspan'=&gt;24, 'class'=&gt;'hetitle']],


  


    ],

// ‘options’=>[‘class’=>‘skip-export’] // remove this row from export

    ]


],    


    'columns' =&gt; [


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


       'headerOptions' =&gt;['class' =&gt; 'table_class','style'=&gt;' text-align: center;width:3.5%','id'=&gt;'remove'],


        'contentOptions' =&gt;['class' =&gt; 'table_class','style'=&gt;' text-align: center;width:3.5%'],  ], 

When am doing view table format am getting output …but when am doning with gird view am getting error--------------------

error

[color=#E51717][font=inherit][size=2]Invalid Configuration[/size][/font][/color] – yii\base\InvalidConfigException

The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.

please someone help me to solve this error