php - Symfony 3 Doctrine Select with Relantionship -


fellows have following function:

 /**      *      * {@inheritdoc}      * @see \appbundle\interfaces\crudmanagerinterface::search()      *      * @return member[]      */     public function search(array $searchparams, array $order , $page, $limit)     {         /**          * @var \doctrine\common\persistence\objectrepository $querybuilder          */         $querybuilder=$this->entitymanager->createquerybuilder();          $querybuilder=$querybuilder->select('m')->from('appbundle:member','m');          if(!empty($searchparams['name']))         {             $querybuilder->andwhere('m.name :name')->setparameter('name','%'.$searchparams['name'].'%');         }          if(!empty($searchparams['schools']))         {             if(!is_array($searchparams['schools']))             {                 $searchparams['schools']=[$searchparams['schools']];             }              $querybuilder->andwhere('m.schools in ( select s.id appbundle:schools s s.id in (:schools ) )')->setparameters(['schools'=>$searchparams['schools']]);         }          if(!empty($order))         {             if(isset($searchparams['name']))             {                 $querybuilder->addorderby('m.name',$order['name']);             }         }          if((int)$limit>0)         {             $querybuilder->setfirstresult((int)$page)->setmaxresults($limit);         }           /**          * @var doctrine\orm\query          */         $query=$querybuilder->getquery();          $querystring=$query->getdql();          $results=$query->getresult();         return $results;     } 

and following entities:

<?php  namespace appbundle\entity;  use doctrine\orm\mapping orm; use appbundle\interfaces\arrayableinterface;  /**  * @orm\entity  * @orm\table(name="members")  */ class member implements arrayableinterface {     /**      * @orm\id      * @orm\column(type="integer")      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @orm\column(type="string", length=100)      */     private $email;      /**      * @var string      * @orm\column(type="string", length=100)      */     private $name;      /**      * @orm\manytomany(targetentity="school",mappedby="members")      * @orm\jointable(name="members_have_schools")      */     private $schools;      /**      * constructor      */     public function __construct()     {         $this->schools = new \doctrine\common\collections\arraycollection();     }      /**      * id      *      * @return integer      */     public function getid()     {         return $this->id;     }      /**      * set email      *      * @param string $email      *      * @return member      */     public function setemail($email)     {         $this->email = $email;          return $this;     }      /**      * email      *      * @return string      */     public function getemail()     {         return $this->email;     }      /**      * add school      *      * @param \appbundle\entity\school $school      *      * @return member      */     public function addschool(\appbundle\entity\school $school)     {         $school->addmember($this);         $this->schools[] = $school;          return $this;     }      /**      * remove school      *      * @param \appbundle\entity\school $school      */     public function removeschool(\appbundle\entity\school $school)     {         $this->schools->removeelement($school);     }      /**      * schools      *      * @return \doctrine\common\collections\collection      */     public function getschools()     {         return $this->schools;     }      /**      * set name      *      * @param string $name      *      * @return member      */     public function setname($name)     {         $this->name = $name;          return $this;     }      /**      * name      *      * @return string      */     public function getname()     {         return $this->name;     }      /**      * {@inheritdoc}      * @see \appbundle\interfaces\arrayableinterface::toarray()      */     public function toarray()     {         $array=['id'=>$this->getid(),'name'=>$this->getname(),'email'=>$this->getemail(),'schools'=>array()];          $schools=$this->getschools()->getvalues();          foreach($schools $school)         {             $array['schools'][]=$school->toarray();         }          return $array;     } } 

and

<?php namespace appbundle\entity;  use doctrine\orm\mapping orm; use appbundle\interfaces\arrayableinterface;  /**  * @orm\entity  * @orm\table(name="schools")  */ class school implements arrayableinterface {     /**      * @var int      * @orm\id      * @orm\column(type="integer")      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      * @orm\column(type="string", length=200)      */     private $name;      /**      *      * @var unknown      */     private $school_id;      /**      * @var unknown      * @orm\manytomany(targetentity="member", inversedby="schools")      */     private $members;      /**      * constructor      */     public function __construct()     {         $this->members = new \doctrine\common\collections\arraycollection();     }      /**      * id      *      * @return integer      */     public function getid()     {         return $this->id;     }      /**      * set name      *      * @param string $name      *      * @return school      */     public function setname($name)     {         $this->name = $name;          return $this;     }      /**      * name      *      * @return string      */     public function getname()     {         return $this->name;     }      /**      * add member      *      * @param \appbundle\entity\member $member      *      * @return school      */     public function addmember(\appbundle\entity\member $member)     {         $this->members[] = $member;          return $this;     }      /**      * remove member      *      * @param \appbundle\entity\member $member      */     public function removemember(\appbundle\entity\member $member)     {         $this->members->removeelement($member);     }      /**      * members      *      * @return \doctrine\common\collections\collection      */     public function getmembers()     {         return $this->members;     }      /**      *      * {@inheritdoc}      * @see \appbundle\interfaces\arrayableinterface::toarray()      */     public function toarray()     {         $array=['id'=>$this->getid(),'name'=>$this->getname()];          return $array;     } } 

with function mentioned on top want search members name , school id given array. theese in $searchparams array. cannot find out way search schoool ids.

an example parammeter $searchparams is:

[   'name'=>'sailor'   'schools'=>[1,2,3] ] 

some example data db is

members:

> id|  name           | email              | > 1 |  sailor mooon   | sailor@moon.com    | > 2 | monk1ey d luffy | monkey.d@luffy.com | 

schools

> id | name  > 1  | kokoro daigaku > 2  | oumi akademy > 3  | univercity of pokemon battle 

in function mentioned on top following error:

: [semantical error] line 0, col 71 near 'id in ( select': error: class appbundle\entity\member has no field or association named schools.id [] []

can me how fix it?

edit 1

i managed solve partiallly changing the:

$querybuilder->andwhere('m.schools in ( select s.id appbundle:schools s s.id in (:schools ) )')->setparameters(['schools'=>$searchparams['schools']]); 

into this:

$querybuilder->join('appbundle:school','s')->andwhere('s.id in (:schools)')->setparameters(['schools'=>$searchparams['schools']]); 

but membeers not relationshiped school.

in end changed the:

$querybuilder->join('appbundle:school','s')->andwhere('s.id in (:schools)')->setparameters(['schools'=>$searchparams['schools']]); 

into this:

$querybuilder->join('m.schools','s')->andwhere('s.id in (:schools)')->setparameters(['schools'=>$searchparams['schools']]); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -