1

I have two tables Project and User and a join table ProjectUser. I am creating a query to select the users under a certain projectName I couldn't do that so I created a query to select the id of the project according to its name from the project table public function findName($projectName){ $query=$this->getEntityManager() ->createQuery("SELECT p.id FROM SocialProProjectBundle:Project p WHERE ``p.name='$projectName'"); return $query->getResult(); }

and then a query to select the users through the project 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();
}

but I always get Notice: Array to string conversion !!!!!

Here is how I called them in the controller

 $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
}
7
  • Also every foreach loop does not return a result !!! when I create an object inside the loop and I use it outside of it I always get undefined variable Commented Feb 12, 2017 at 13:57
  • $name is an array and you try to display it: <option>$name</option>. Commented Feb 12, 2017 at 14:47
  • @malcolm I've tried it this way foreach ($pus as $user) {//($i=0;$i<count($pus);$i++) $name=$user->getFirstName(); $response = $response . "<option>$name</option>"; } and still the same problem Commented Feb 12, 2017 at 15:04
  • The Notice says that the error is in the query Commented Feb 12, 2017 at 15:09
  • Because your $pId is a object or array not string. Commented Feb 12, 2017 at 15:29

2 Answers 2

1

Hi: for the for loop question you had on how to solve it, use this code:

$response = "<select class=\"select2_multiple form-control\" multiple=\"multiple\">";
foreach($pus as $user){
    $response . "<option>" . $user->getFirstName() . "</option>";
}
$response = $response."</select> ";
Sign up to request clarification or add additional context in comments.

Comments

0

The solution I found to this is that I avoided the $pId and found the query capable to select the users through the $projectName Here it Is :

 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();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.