ZF2 更改我的表达式并返回错误(Postgre,Distinct)
ZF2 changing my expression and returning error (Postgre, Distinct)
我必须创建一个 SELECT,我将在其中按列 idcontrato 区分行。我用 SQL...
做得很好
SELECT DISTINCT ON (idcontrato) *
FROM cad_emprestimo
WHERE numerobeneficio = '1135346515';
但是当我尝试对 ZF2 执行相同操作时:
$emprestimos = (new EmprestimoTable())->select(function(Select $select) use($cliente) {
$select->columns([new Expression('DISTINCT ON (idcontrato) *')]);
$select->where->equalTo('numerobeneficio', $cliente->getBeneficio()->getNumero());
});
我收到以下错误:
SQLSTATE[42601]: Syntax error: 7 ERRO: erro de sintaxe em ou próximo a "AS"
LINE 1: SELECT DISTINCT ON (idcontrato) * AS Expression1 FROM "cad_e...
^
这个'AS Expression1'由ZF2添加...我不知道如何删除它。
试试下面的代码
$emprestimos = (new EmprestimoTable())->select(function(Select $select) use($cliente) {
$select->columns([new Expression('DISTINCT(idcontrato) AS idcontrato'), '*']);
$select->where->equalTo('numerobeneficio', $cliente->getBeneficio()->getNumero());
});
我找到的明显解决方案是 运行 原始 SQL:
$dbAdapter = GlobalAdapterFeature::getStaticAdapter();
$numBeneficio = $cliente->getBeneficio()->getNumero();
$sqlEmprestimos = "SELECT DISTINCT ON (idcontrato) * FROM cad_emprestimo WHERE numerobeneficio = '".$numBeneficio."'; ";
$dataSource = $dbAdapter->createStatement($sqlEmprestimos)->execute();
$emprestimos = (new \Zend\Db\ResultSet\ResultSet())->initialize($dataSource);
无论如何,我对此并不满意,这段代码丑得要命。
我必须创建一个 SELECT,我将在其中按列 idcontrato 区分行。我用 SQL...
做得很好SELECT DISTINCT ON (idcontrato) *
FROM cad_emprestimo
WHERE numerobeneficio = '1135346515';
但是当我尝试对 ZF2 执行相同操作时:
$emprestimos = (new EmprestimoTable())->select(function(Select $select) use($cliente) {
$select->columns([new Expression('DISTINCT ON (idcontrato) *')]);
$select->where->equalTo('numerobeneficio', $cliente->getBeneficio()->getNumero());
});
我收到以下错误:
SQLSTATE[42601]: Syntax error: 7 ERRO: erro de sintaxe em ou próximo a "AS"
LINE 1: SELECT DISTINCT ON (idcontrato) * AS Expression1 FROM "cad_e...
^
这个'AS Expression1'由ZF2添加...我不知道如何删除它。
试试下面的代码
$emprestimos = (new EmprestimoTable())->select(function(Select $select) use($cliente) {
$select->columns([new Expression('DISTINCT(idcontrato) AS idcontrato'), '*']);
$select->where->equalTo('numerobeneficio', $cliente->getBeneficio()->getNumero());
});
我找到的明显解决方案是 运行 原始 SQL:
$dbAdapter = GlobalAdapterFeature::getStaticAdapter();
$numBeneficio = $cliente->getBeneficio()->getNumero();
$sqlEmprestimos = "SELECT DISTINCT ON (idcontrato) * FROM cad_emprestimo WHERE numerobeneficio = '".$numBeneficio."'; ";
$dataSource = $dbAdapter->createStatement($sqlEmprestimos)->execute();
$emprestimos = (new \Zend\Db\ResultSet\ResultSet())->initialize($dataSource);
无论如何,我对此并不满意,这段代码丑得要命。