我正在使用NativeSQL,我需要设置一个未定义数量的参数。我的代码:
$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();
我需要设置每个查询参数的类型,但setParameters()函数不允许传递这种类型。
我看到AbstractQuery(Doctrine类)的setParameter()函数允许传递这种类型,但是我想使用setParamaters()函数,因为我必须传递一个未定义数量的参数...
我该如何解决这个问题?感谢。
答案 0 :(得分:0)
您可以使用以下内容将多个参数传递给查询:
$entityManager = $this->getEntityManager();
$sql = "
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)
$stmt = $entityManager->getConnection()->prepare($sql);
$stmt->execute([
't1_name' => 'Andy',
t2_age' => 12,
...
]);
$results = $stmt->fetchAll();
答案 1 :(得分:0)
使用此solution一切都很完美!