Orm: Native SQL: set types of multiple query parameters

Created on 24 Nov 2017  路  2Comments  路  Source: doctrine/orm

I'm using NativeSQL and I need set an undefined number of parameters. My code:

$entityManager = $this->getEntityManager();
$rsm = new Query\ResultSetMappingBuilder($entityManager);
$mySql = "
   SELECT *
   FROM table1 t1
   INNER JOIN table2  t2 ON t2.t1_id = t1.id
   ...
   WHERE t1.name = :t1_name AND t2.age = :t2_age AND ...
";
// array of multiple query parameters (name, value)
$queryParameters = array(
   't1_name' => 'Andy',
   't2_age' => 12,
   ...
);
$query = $entityManager->createNativeQuery($mySql, $rsm);
$query->setParameters($queryParameters);
$salidas = $query->getResult();

I need to set the type of every query parameters but setParameters() function doesn't allow pass this types.

I see that setParameter() function of AbstractQuery (Doctrine class) allow pass this types, but I would like to use setParamaters() function because I have to pass an undefined number of paramaters...

How can I solve this? Thanks.

Most helpful comment

The way you use setParameters is the old way.
After https://github.com/doctrine/doctrine2/pull/360 (from 2012) you should define them using Parameter objects:

$queryParameters = new ArrayCollection(array(
   new Parameter('t1_name', 'Andy', TYPE::STRING),
   new Parameter('t2_age', 12, TYPE::INTEGER),
   ...
));

All 2 comments

The way you use setParameters is the old way.
After https://github.com/doctrine/doctrine2/pull/360 (from 2012) you should define them using Parameter objects:

$queryParameters = new ArrayCollection(array(
   new Parameter('t1_name', 'Andy', TYPE::STRING),
   new Parameter('t2_age', 12, TYPE::INTEGER),
   ...
));

It works perfectly. Thanks!

Was this page helpful?
0 / 5 - 0 ratings