php 如何在yii2中实现多个左连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24551669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:21:28  来源:igfitidea点击:

How to achieve muliple left join in yii2?

phpyii2

提问by mohi

I have 3 tables named stock, product, users. I have a joinning query as below. Can anybody please tell me how to implement it in yii2?

我有 3 个名为 stock、product、users 的表。我有一个联接查询,如下所示。谁能告诉我如何在yii2中实现它?

SELECT 
    stock.sale_price_per_unit,
    sum(stock.quantity),
    product.product_name,
    users.username

FROM
    stock
LEFT JOIN product ON product.product_id=stock.product_id
LEFT JOIN users ON users.user_id=stock.seller_id
WHERE stock.product_id=3
GROUP BY stock.seller_id

回答by user1852788

Refering to http://www.yiiframework.com/doc-2.0/guide-active-record.html#joining-with-relationsand http://www.yiiframework.com/forum/index.php/topic/50423-joins-are-back/:

参考http://www.yiiframework.com/doc-2.0/guide-active-record.html#joining-with-relationshttp://www.yiiframework.com/forum/index.php/topic/50423-加入返回/

$orders = Order::find()->joinWith('customer')->orderBy('customer.id, order.id')->all();

so your code should look like:

所以你的代码应该是这样的:

$query = Stock::find()
->joinWith(['product', 'users']);

$items = $query
    ->select([
        'sale_price_per_unit',
        'sum(quantity)',
        'product.product_name',
        'users.username'])
    ->where(['stock.product_id'=>3])
    ->groupBy('seller_id')
    ->all();

where relations "product" and "users" declared in Stock model: HAS_MANY:

在 Stock 模型中声明的关系“产品”和“用户”: HAS_MANY:

public function getUsers()
{
  return $this->hasMany(User::className(), ['seller_id' => 'user_id']);
}

HAS_ONE

HAS_ONE

public function getProduct()
{
  return $this->hasOne(Product::className(), ['product_id' => 'product_id']);
}

回答by Ashish pathak

Here is full code For joins In model Geography.phphere i have model geography and i bind three tables in geography table in yii2

这是连接模型Geography.php 的完整代码, 这里我有模型地理,我在 yii2 的地理表中绑定了三个表

#####**Create Relation With Country Table** 

public function getCountry() {
    return $this->hasOne(Country::className(), ['id' => 'country_id']);
}

#####**Create Relation With State Table** 

public function getState() {
    return $this->hasOne(State::className(), ['id' => 'state_id']);
}

**#####Create Relation With City Table** 

public function getCity() {
    return $this->hasOne(City::className(), ['id' => 'city_id']);
}

回答by Samar Haider

    $criteria = new CDbCriteria;
$criteria->select = 'DISTINCT t.*';
                                        $criteria->join = ' LEFT JOIN `shared_items` AS `si` ON t.workspace_id = si.workspace_id';
                                        $criteria->addCondition('t.status_id != ' . ProjectStatuses::STATUS_CLOSED);
                                        $criteria->addCondition('t.workspace_id = ' . $workspace_id);