Yii Framework Forum: Cannot access field from join query in search model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cannot access field from join query in search model

#1 User is offline   amaliazakiyah 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 06-December 17

Posted 06 December 2017 - 07:42 PM

Hallo, i'm newbie in yii2 framework. Now, i'm trying to create data provider in search model using relations and some leftJoin() method.
But, when I add $this->field_from_leftJoin in filterWhere, always getting error unknown property.
And here my SearchModel :
public function search($params)
    {
        $query = ScheduleProduction::find();
        $query->joinWith('idCircuit')
                ->joinWith('idSchedule')
                ->leftJoin('detail_order', 'detail_order.id_detail_order = schedule.id_detail_order')
                ->leftJoin('order_project', 'order_project.id_order = detail_order.id_order')
                ->where([
                        'order_project.id_car' => Yii::$app->session->get('carmaker'),
                        'order_project.id_shift' => Yii::$app->session->get('shift'),
                ]);

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                                'pageSize' => 10,
                            ],
        ]);

        $dataProvider->sort->attributes['id_order'] = [
            'asc' => ['order_project.id_order' => SORT_ASC],
            'desc' => ['order_project.id_order' => SORT_ASC]
        ];

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        if (Yii::$app->session->get('cutoff') == 1 || Yii::$app->session->get('cutoff') == 2 || Yii::$app->session->get('cutoff') == 5 || Yii::$app->session->get('cutoff') == 6 ) {
            $query->andFilterWhere([
                    'workdate' => Yii::$app->session->get('workdate')
                ]);
        }

        if (Yii::$app->session->get('cutoff') == 3 || Yii::$app->session->get('cutoff') == 7) {
            $query->andFilterWhere(['order_project.id_order' => $this->id_order])
                  ->andFilterWhere(['or',
                        ['circuit.process' => 'S-JOINT'],
                        ['circuit.process' => 'TWIST'],
                        ['circuit.process' => 'BONDER'],
                        ['circuit.process' => 'SHIELD']
                        ]);
        }

        if (Yii::$app->session->get('cutoff') == 4 || Yii::$app->session->get('cutoff') == 8) {
            $query->andFilterWhere(['order_project.id_order' => $this->id_order])
                  ->andFilterWhere([
                        'circuit.process' => 'SUB',
                    ]);
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id_sch_prod' => $this->id_sch_prod,
            'id_schedule' => $this->id_schedule,
            'id_detail_order' => $this->id_detail_order,
            'prod_qty' => $this->prod_qty,
            'scan_quantity' => $this->scan_quantity,
        ]);

        $query->andFilterWhere(['like', 'id_assy', $this->id_assy])
            ->andFilterWhere(['like', 'barcode_kanban', $this->barcode_kanban])
            ->andFilterWhere(['like', 'barcode_group', $this->barcode_group]);

        return $dataProvider;
    }


It alwasy getting unknwon property ::id_order.
$query->andFilterWhere(['order_project.id_order' => $this->id_order])

And it also error when I call property from relations.
Please help me..
Thank you..
0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,483
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 06 December 2017 - 09:29 PM

I don't think your sql is causing Unknown property error, it is due to your $this->id_order do you have that attribute in your model, where does that come from?
0

#3 User is offline   wilson_xyz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 18-June 16

Posted 07 December 2017 - 12:02 AM

do you have relation within these 3 tables? if you have, you could just using
joinWith(['idCircuit', 'idSchedule', 'detail_order', 'order_project'])

0

#4 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 159
  • Joined: 10-October 11
  • Location:Russia

Posted 07 December 2017 - 03:34 AM

it might help
$query = ScheduleProduction::find();
$query->joinWith('idCircuit')
    ->joinWith('idSchedule')
    ->leftJoin('detail_order', 'detail_order.id_detail_order = schedule.id_detail_order')
    ->leftJoin('order_project', 'order_project.id_order = detail_order.id_order')
    ->where([
        'order_project.id_car' => Yii::$app->session->get('carmaker'),
        'order_project.id_shift' => Yii::$app->session->get('shift'),
    ]);
echo '<pre>';
VarDumper::dump($query->createCommand()->sql);
echo '<pre>';
Yii::$app->end();

to see the query in a raw way
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users