Native SQL: set types of multiple query parameters #5783

Closed
opened 2026-01-22 15:17:46 +01:00 by admin · 2 comments
Owner

Originally created by @avg0043 on GitHub (Nov 24, 2017).

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.

Originally created by @avg0043 on GitHub (Nov 24, 2017). 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.
admin closed this issue 2026-01-22 15:17:46 +01:00
Author
Owner

@mateuszsip commented on GitHub (Dec 3, 2017):

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),
   ...
));
@mateuszsip commented on GitHub (Dec 3, 2017): 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), ... )); ```
Author
Owner

@avg0043 commented on GitHub (Dec 4, 2017):

It works perfectly. Thanks!

@avg0043 commented on GitHub (Dec 4, 2017): It works perfectly. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5783