Search code examples
sqlyii2inner-join

Yii2 innerJoin()


I want to implement a sql query in the following way:

INNER JOIN
`Product_has_ProductFeature` t ON `Product`.`id` = t.`productId` AND t.`productFeatureValueId` = 1
INNER JOIN
`Product_has_ProductFeature` t1 ON `Product`.`id` = t1.`productId` AND t1.`productFeatureValueId` = 5

How can I do this using innerJoin() or something like above mentioned?


Solution

  • innerJoin() is a method from the Query class.

    You can try something like this.

    $query = new \yii\db\Query;
    $command = $query->innerJoin(
             'Product_has_ProductFeature',
             `Product`.`id` = t.`productId`)
         ->andWhere('t.`productFeatureValueId` = 1')
         ->createCommand();
    $queryResult = $command->query();