i trying run raw sql query doctrine entitymanager in clause shown below.
$idsarray = array ( [0] => 1 [1] => 2 ) $stmt = $this->getdoctrine()->getentitymanager() ->getconnection() ->prepare('select t1.id , t1.name , t2.start_date , t2.end_date table1 t1 , table2 t2 t1.id = t2.matchid , t1.id in (:ids)'); $params = array( 'ids' => implode( ",", $idsarray ) ); $stmt->execute($params); $results = $stmt->fetchall();
but getting result id = 1. if hardcode in condition as
t1.id = t2.matchid , t1.id in (1,2)');
then getting result both ids. can tell me doing wrong in passing $params array. have print implode result outputs 1,2. not able find mistake , way perform raw sql query in clause.
answer:
so there @ least 2 mistakes did. first @alarid said: should not implode array. second have use doctrinedbaltypes conversion
in clause
when running prepared statement.
and query goes this:
$stmt = $this->getdoctrine()->getentitymanager() ->getconnection() ->prepare('select t1.id , t1.name , t2.start_date , t2.end_date table1 t1 , table2 t2 t1.id = t2.matchid , t1.id in (:ids)'); $stmt->bindvalue('ids', $idsarray, \doctrine\dbal\connection::param_int_array); $stmt->execute();
or alternative:
$stmt = $this->getdoctrine()->getentitymanager() ->getconnection() ->executequery('select t1.id , t1.name , t2.start_date , t2.end_date table1 t1 , table2 t2 t1.id = t2.matchid , t1.id in (:ids)', array('ids' => $idsarray), array('ids' => \doctrine\dbal\connection::param_int_array) ) ;
Comments
Post a Comment