Search with left join

Hello,

I’m having troubles when I try to display left joined fields inside view, since they’re not in the model I search in. What would you suggest me to do?

Here’s what I have:

Model of AvailabilityCalendarSearch (Where I’m doing the join):

public function search($params)

{


    $query = AvailabilityCalendar::find();





    $dataProvider = new ActiveDataProvider([


        'query' => $query,


		'sort' => [


            'defaultOrder' => [


                'ac_price' => SORT_ASC,


            ]


        ],


    ]);





    if (!($this->load($params) && $this->validate())) {


        return $dataProvider;


    }


	


    $query->andFilterWhere([


        'ac_id' => $this->ac_id,


        'ac_owner_id' => $this->ac_owner_id,


        'ac_position' => $this->ac_position,


        'ac_date' => $this->ac_date,


        //'ac_start_time' => $this->ac_start_time,


        'ac_price' => $this->ac_price,


        'ac_status' => $this->ac_status,


    ]);


	$query->andFilterWhere(['<', 'ac_start_time', $this->ac_start_time]);


	$query->leftJoin('employees', 'availability_calendar.ac_owner_id  = employees.employee_user_id');


    return $dataProvider;


}

Controller Search :

public function actionSearchemployees()

{


	$model = new AvailabilityCalendarSearch;


	$svar = 0;


	echo "xpar=".print_r(Yii::$app->request->queryParams);


	if ($model->load(Yii::$app->request->get())) {


		//echo "xpost";


		$svar = 1;


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


		


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


		'model' => $model,


        'dataProvider' => $dataProvider,


		'svar' =>$svar


    ]);


    } else {


		//echo "xpost2";


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


        'model' => $model,


		'svar' =>$svar


    ]);


    }


	   


}

and I’m trying to display joined field employee_firstname

<?= GridView::widget([

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


    'columns' =&gt; [


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





        'ac_position',


		'ac_date',


		'ac_start_time',


		'ac_price',


		'employee_firstname',





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


    ],


]); }?&gt;

it says that employee_firstname is not a part of the model… which is right :)

I just don’t know how to implement it, please advice!

Thank you!!!

Instead using leftJoin, try to

  1. make a relation in AvailabilityCalendar, so:



    public function getEmployee()

    {

        // Order has_one Customer via Customer.id -> customer_id

        return $this->hasOne(Employees::className(), ['employee_user_id' => 'ac_owner_id']);

    }



  1. At bottom of search() change



$query->leftJoin('employees', 'availability_calendar.ac_owner_id = employees.employee_user_id');



with




$query->with('employees');



  1. Change your gridview with



<?= GridView::widget([

'dataProvider' => $dataProvider,

'columns' => [

['class' => 'yii\grid\SerialColumn'],


'ac_position',

'ac_date',

'ac_start_time',

'ac_price',

'employee.employee_firstname',


['class' => 'yii\grid\ActionColumn'],

],

]); }?>



Gosh, this is genious! It worked flawlessly!! Thank you so much!!!!!!