我正在使用nativeql,我需要设置一个不确定的参数。我的代码:
$entityManager = $this->getEntityManager();
$rsm = new QueryResultSetMappingBuilder($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(学说类)的SetParameter()函数允许通过此类型,但是我想使用setParamaters()函数,因为我必须传递一个未定义的参数数量...
我该如何解决?谢谢。
您可以使用以下内容将多个参数传递给查询:
$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();
参考数据检索和操作