简体   繁体   English

数组到字符串的转换SYmfony SQL

[英]Array to string conversion SYmfony Sql

I have an sql request: 我有一个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();

but fetchColumn return just one result, and I want a set of list user id to use it in the next request. 但是fetchColumn仅返回一个结果,并且我希望一组列表用户ID在下一个请求中使用它。

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

but this return just one result. 但这只会返回一个结果。 Any Idea to replace fetchColumn by other request or using doctrince. 任何通过其他请求或使用文档替换fetchColumn的想法。

With your logic, you can simply fetch all your users and implode it. 按照您的逻辑,您可以轻松获取所有用户并使其内爆。

$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 :'(

Fetch colum will only return the column of the next row . 获取列将仅返回下一行的列 So for your second piece of code, you can loop over results. 因此,对于第二段代码,您可以遍历结果。

But, if you're are using a framework like symfony, you have many other ways to make it cleaner. 但是,如果您使用的是诸如symfony之类的框架,则可以通过许多其他方法使其变得更整洁。 Just check out the symfony doc to use repository relations and more generally doctrine 只需查看symfony文档即可使用repository relations和更一般的doctrine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM