symfony 1.4 - Using Optional Parameters in where clause in symfony2 -
how can utilize optional parameters clause of select query using symfony query builder.
ex.
in php,
$userid = (empty())?' userid='.$userid:''
qry : "select * users ".$userid
similarly, in symfony, how can utilize optional parameter in clause using createquerybuilder????
somewhere in repository or create qb:
// works in entityrepository $qb = $this->createquerybuilder(); // otherwise $em instanceof entitymanager // $qb = $em->createquerybuilder() $qb ->select('ss, ls') ->innerjoin('ss.linksomething', 'ls') ->where('ss.fieldone = 1') ; // $one first status if($one){ $qb->andwhere('ss.fieldtwo = 2'); } // $two sec status if($two){ $qb->andwhere('ls.fieldone = 1'); }
just create querybuilder instance 1 time , start add together joins or wheres in combination want entities select , joins made.
after don't forget to:
//setup params, if they're in query $qb->setparameters($arrparam); //to check sql // $sql = $qb->getquery()->getsql(); //to check dql // $dql = $qb->getquery()->getdql(); //result $res = $qb->getquery()->getresult();// result method need doctrine documentation
upd: more info read documentation, it's total of easy examples.
symfony2 symfony-1.4
No comments:
Post a Comment