Yii Framework Forum: Issue using relation in where clause of ActiveQuery - Yii Framework Forum

Jump to content

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

Issue using relation in where clause of ActiveQuery

#1 User is offline   sills 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-February 18

Posted 19 February 2018 - 10:58 AM

In attempting to use the following ActiveQuery
           $query = HpcSoftwareGroupMembers::find()->joinWith(['group'])->where(['group.RESTRICTED' \
=> null])->all();
           $dataProvider = new ActiveDataProvider([
              'query' => $query,
           ]);


I get a database exception. The SQL that was generated is

The SQL being executed was:
SELECT "HPC_SOFTWARE_GROUP_MEMBERS".* FROM "HPC_SOFTWARE_GROUP_MEMBERS" LEFT JOIN "HPC_SOFTWARE_GROUPS" ON "HPC_SOFTWARE_GROUP_MEMBERS"."GROUP_NAME" = "HPC_SOFTWARE_GROUPS"."GROUP_NAME" WHERE "group"."RESTRICTED" IS NULL

The group relation in joinWith was interpreted correct, but in the where statement group doesn't seem to have been
recognized as a relation?
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,639
  • Joined: 16-February 11
  • Location:Japan

Posted 19 February 2018 - 11:10 AM

Try the following:
    $query = HpcSoftwareGroupMembers::find()->joinWith(['group'])
        ->where(['HPC_SOFTWARE_GROUPS.RESTRICTED' => null])
        ->all();


Or, if you want to use an alias for the related table:
    $query = HpcSoftwareGroupMembers::find()->joinWith(['group g'])
        ->where(['g.RESTRICTED' => null])
        ->all();


Please look at the API reference of joinWith(). The first parameter $with can can specify the table alias.
http://www.yiiframew....html#joinWith()-detail
0

#3 User is offline   sills 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-February 18

Posted 19 February 2018 - 11:23 AM

Either of those results in error

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

which, as far as I can tell, occurs before the query is attempted?
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,639
  • Joined: 16-February 11
  • Location:Japan

Posted 19 February 2018 - 06:41 PM

    $query = HpcSoftwareGroupMembers::find()->joinWith(['group'])
        ->where(['HPC_SOFTWARE_GROUPS.RESTRICTED' => null]);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);


Or,
    $query = HpcSoftwareGroupMembers::find()->joinWith(['group g'])
        ->where(['g.RESTRICTED' => null]);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

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