think-orm icon indicating copy to clipboard operation
think-orm copied to clipboard

before_find有问题返回的数据是一个数组.所以导致错误.

Open momoca opened this issue 4 years ago • 6 comments

https://github.com/top-think/think-orm/blob/0b01a19654c3b0376043960a30e74c9ddb55d574/src/db/PDOConnection.php#L868

测试代码

\think\facade\Db::event('before_find', function ($query) {
    return false;
});

$result = $this->db->trigger('before_find', $query);返回结果

Array
(
    [0] => 
)

所以这个代码块永远不会被执行. https://github.com/top-think/think-orm/blob/0b01a19654c3b0376043960a30e74c9ddb55d574/src/db/PDOConnection.php#L870

所以这个代码块永远不会被执行.

希望官方尽快解决一下.

我的修改方式是去除空数组

$result = $this->db->trigger('before_find', $query);
$result = array_filter($result);

momoca avatar Jun 24 '21 11:06 momoca

我试了直接返回return $result;也不行.下边根本处理不了.

$result = $this->db->trigger('before_find', $query);
$result = $result[0] ?? [];//处理多维

试了试后感觉这样写比较好,不管是返回false还是result都ok. @liu21st

momoca avatar Jun 24 '21 12:06 momoca

before_select 也有同样的问题.同样加$result = $result[0] ?? [];//处理多维可解决

momoca avatar Jun 25 '21 09:06 momoca

查询事件已经更新了,用法已经限定 https://www.kancloud.cn/manual/thinkphp6_0/1037571

augushong avatar Aug 23 '21 05:08 augushong

查询事件已经更新了,用法已经限定 https://www.kancloud.cn/manual/thinkphp6_0/1037571

看到这句话“不需要在事件中返回任何东西,不要在事件中进行select操作”我差点回到20年前,真的,太奇葩了

AnkoGo123 avatar Nov 07 '22 00:11 AnkoGo123

你看看这是啥: https://www.kancloud.cn/manual/thinkphp6_0/1037598

augushong avatar Nov 07 '22 00:11 augushong

数据库的事件跟模型的事件是两套系统,面对的意义不一样。

数据库的事件主要是对数据库操作的中间件拦截之类的操作,或者做一套统一的封装。

模型的事件支持各类业务操作,在这里面写图像处理写爬虫也没人管。

augushong avatar Nov 07 '22 00:11 augushong