数组到字符串转换SYmfony Sql
问题描述:
I have an sql request:
$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.
$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.
我有一个sql请求: p>
$ 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();
code> pre>
但是fetchColumn只返回一个结果,我想要一个集合 列表用户ID在下一个请求中使用它。 p>
$ listPush = $ connection-> prepare(“从Table_name中选择id,其中id_user在(?)中); \ n $ listPush-> bindValue(1,$ users);
$ listPush-> execute();
$ idpushs = $ listPush-> fetchColumn();
code> pre> \ n
但这只返回一个结果。任何想法用其他请求或使用doctrince替换fetchColumn。 p>
div>
答
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. Just check out the symfony doc to use repository
relations
and more generally doctrine