Iso*_*lde 3 select max zend-framework2
我正在使用ZendFramework 2和TableGateway,它适用于普通的select语句.但我似乎无法找到如何使用ZendFramework 2选择获得最大列.我的选择看起来应该是这样的
SELECT MAX( `publication_nr` ) AS maxPubNr FROM `publications`
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像:
use Zend\Db\TableGateway\TableGateway;
class PublicationsTable
{
protected $tableGateway;
...
public function getMaxPubicationNr()
{
$rowset = $this->tableGateway->select(array('maxPubNr' => new Expression('MAX(publication_nr)')));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not retrieve max Publication nr");
}
return $row;
}
Run Code Online (Sandbox Code Playgroud)
如果查看TableGateway,您会注意到Select方法的参数实际上是传递给Query的Where部分,这使得查询不正确.
您需要直接修改Select对象,因为TableGateway不会为您提供任何代理方法.
你可以尝试这样的事情:
public function getMaxPubicationNr()
{
$select = $this->tableGateway->getSql()->select();
$select->columns(array(
'maxPubNr' => new Expression('MAX(publication_nr)')
));
// If you need to add any where caluses you would need to do it here
//$select->where(array());
$rowset = $this->tableGateway->selectWith($select);
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not retrieve max Publication nr");
}
return $row;
}
Run Code Online (Sandbox Code Playgroud)
我没有测试过,但这应该只是让你在那里:)
你可能会遇到另一个问题,那将是由于TableGateway试图从结果中构建一个对象,但你没有带回一整行来构建一个对象,你只是带回一个柱.
我只是添加使用Db/Select对象来做到这一点,而不是打扰GateWay说实话,我不认为它应该像这样使用.