数组到字符串转换错误Symfony 3

时间:2017-02-12 13:55:43

标签: mysql symfony

我有两个表Project和User以及一个连接表ProjectUser。我正在创建一个查询来选择某个projectName下的用户我不能这样做所以我创建了一个查询,根据项目表中的名称选择项目的id             public function findName($projectName){ $query=$this->getEntityManager() ->createQuery("SELECT p.id FROM SocialProProjectBundle:Project p WHERE ``p.name='$projectName'"); return $query->getResult(); }

然后通过项目ID

选择用户的查询
public function findProjectUsers($pId){
 $query=$this->getEntityManager()
 ->createQuery(
 "SELECT pu, u FROM SocialProProjectBundle:ProjectUser pu JOIN      SocialProDefaultBundle: User u WHERE pu.project = '$pId'"
        );
    return $query->getResult();
}

但我总是注意到:数组转换为字符串!!!!!

以下是我在控制器中调用它们的方式

 $projectName = $request->get('projectName');
    echo($projectName);
    $projectId=$this->getDoctrine()->getRepository('SocialProMeetingBundle:meetingUser')->findName($projectName);
    echo(count($projectId));
    foreach($projectId as $pId) {
        $pus = $this->getDoctrine()->getRepository('SocialProMeetingBundle:meetingUser')->findProjectUsers($pId);
    }
   $response = "<select class=\"select2_multiple form-control\" multiple=\"multiple\">";
    foreach ($pus as $user) {//($i=0;$i<count($pus);$i++)
        $name[]=array($user->getFirstName());
     }
    $response = $response . "<option>$name</option>";
    $response = $response."</select> ";
    return new Response($response);
    //return($pus);
    //call repository function
}

2 个答案:

答案 0 :(得分:1)

嗨:关于如何解决问题的for循环问题,请使用以下代码:

$response = "<select class=\"select2_multiple form-control\" multiple=\"multiple\">";
foreach($pus as $user){
    $response . "<option>" . $user->getFirstName() . "</option>";
}
$response = $response."</select> ";

答案 1 :(得分:0)

我发现这个解决方案是我避免了$ pId并发现查询能够通过$ projectName选择用户这里是:

 public function findProjectUsers($projectName){
    $query=$this->getEntityManager()
        ->createQuery(
            "SELECT u.firstName FROM SocialProDefaultBundle:User u, SocialProProjectBundle:Project p, SocialProProjectBundle:ProjectUser pu WHERE u.id = pu.user AND p.id=pu.project AND p.name='$projectName'");
    return $query->getResult();
}