fetchAllAssociative()` 的替代方案并按原则执行

Alternative for fetchAllAssociative()` and execute in doctrine

我正在通过学​​说使用本机 sql 调用存储过程。 fetchAllAssociative()execute 已弃用。有哪些替代方案?

    $sql = 'CALL spWithParams(:param)';
    $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
    $stmt->execute([":param"=>"test"]);
    print_r($stmt->fetchAllAssociative());

我不打算使用 ResultSetMapping() 将响应映射到实体,如前所述 here,因为我将在其上添加我的自定义包装。

正确的做法是使用具有 scalar results 到 select 任意字段的 ResultSetMapping。然后,您可以使用 getArrayResult() 执行查询,以实现与提供的代码相同的行为。

举个例子:

$rsm = new ResultSetMapping();
$rsm->addScalarResult('my_database_column', 'myField');
$query = $this->getEntityManager()->createNativeQuery(
    'CALL spWithParams(:param)',
    $rsm
);
$query->setParameters([":param"=>"test"]);

$results = $query->getArrayResult();
/* 
[
  ['myField' => 'foo'],
  ['myField' => 'bar']
]
*/