gpt4 book ai didi

php - 数组到字符串转换 SYmfony Sql

转载 作者:行者123 更新时间:2023-11-29 11:51:59 26 4
gpt4 key购买 nike

我有一个sql请求:

 $listUsers=$connection->prepare("SELECT u.id as userid From user u , account a where (a.id_parent=$id_account OR  a.id=$id_account) and u.id_account=a.id ");
$listUsers->execute();
$users=$listUsers->fetchColumn();

但是 fetchColumn 仅返回一个结果,我想要一组列表用户 ID 在下一个请求中使用它。

 $listPush=$connection->prepare("select id from Table_name where id_user in (?));
$listPush->bindValue(1, $users);
$listPush->execute();
$idpushs=$listPush->fetchColumn();

但这仅返回一个结果。任何通过其他请求或使用 dotrince 替换 fetchColumn 的想法。

最佳答案

按照您的逻辑,您可以简单地获取所有用户并将其内爆。

$listUsers=$connection->prepare("SELECT u.id as userid From user u , account a where (a.id_parent=$id_account OR  a.id=$id_account) and u.id_account=a.id ");
$listUsers->execute();
$users=$listUsers->fetchAll();
$userIds = array();
foreach ($users as $user) {
$userIds[] = $user['userid'];
}
$users = implode(',', $userIds); //that hurts me so hard to code like this with symfony :'(

获取列只会返回下一行的列。因此,对于第二段代码,您可以循环结果。

但是,如果您使用的是像 symfony 这样的框架,您还有许多其他方法可以使其更简洁。只需查看 symfony 文档即可使用 repository relations 以及更一般的 doctrine

关于php - 数组到字符串转换 SYmfony Sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33912686/

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