gpt4 book ai didi

yii2 - Yii 2 : useles query performed

转载 作者:行者123 更新时间:2023-12-04 03:02:11 25 4
gpt4 key购买 nike

我有一个数据表,其中 product_id 指向 product.id

在 ProductQuery 中,扩展 ActiveQuery,我创建了这些函数

public function all($db = null)
{
return parent::all($db);
}

public function not_g00()
{
$this->andWhere('category_code <> "G00"');
return $this;
}


public function without_datasheet()
{
$this->joinWith('datasheet');
$this->andWhere(['{{%datasheet}}.id'=> null]);
return $this;
}

public function from_newest()
{
$this->addOrderBy(['created_on' => SORT_DESC]);
return $this;
}

虽然find实际上被覆盖到了Product类

public static function find()
{
return new \frontend\models\query\ProductQuery(get_called_class());
}

我用 as :

$products_without_datasheet = Product::find()
->not_g00()
->without_datasheet()
->from_newest()
->all();

结果是第一个查询,简直完美:

SELECT `tbl_product`.* FROM `tbl_product` LEFT JOIN `tbl_datasheet` 
ON `tbl_product`.`id` = `tbl_datasheet`.`product_id`
WHERE (category_code <> "G00") AND (`tbl_datasheet`.id IS NULL)
ORDER BY `created_on` DESC

但是调试日志显示在此之后执行了第二个查询。

SELECT * FROM `tbl_datasheet` WHERE `product_id` IN (7541, 7929,... )

调试显示此查询由 datasheet::find()->all()... 调用,但为什么呢?!,由 all() 调用

$products_without_datasheet = Product::find()
->not_g00()
->without_datasheet()
->from_newest()
->all();

为什么?我究竟做错了什么?为什么在第一个查询已经完美时执行 2 个查询?

最佳答案

你正在使用 $this->joinWith('datasheet');,如果你不想获取相关的数据表,你应该简单地使用 $this->joinWith('datasheet ', 假);

关于 joinWith() :

If the $eagerLoading parameter is true, the method will also eager loading the specified relations, which is equivalent to calling with() using the specified relations.

阅读更多关于 joinWith() 的信息.

关于yii2 - Yii 2 : useles query performed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32506710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com