简体   繁体   English

Zend Framework 2 TableGateway MAX查询

[英]Zend Framework 2 TableGateway MAX query

since few days i have a problem with mysql query using tablegateway and zf2 functions. 由于几天以来,我对使用tablegateway和zf2函数的mysql查询有问题。

I would like to get max value of column 'reservation_spot' where 'reservation_datetime' = &reservation_datetime The query shoud be like this 我想获得列'reservation_spot'最大值,其中'reservation_datetime' = &reservation_datetime查询应该像这样

SELECT MAX(`reservation_spot`) FROM `reservation` WHERE `reservation_datetime`='2015-09-30 8:00'

I've tried many things to solve that problem but i can't 我已经尝试了很多方法来解决该问题,但是我做不到

This is my function 这是我的职责

public function getMaxValueWhereDate($reservation_datetime)
    {
        $select = $this->tableGateway->getSql()->select();
        $select->columns(array(
            'maxValue' => new Expression('MAX(reservation_spot)')
        ));
        $select->where(array('reservation_datetime' => $reservation_datetime));

        $rowset = $this->tableGateway->selectWith($select);
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not retrieve max value");
        }

        return $row;
    }

plus this 再加上这个

$reservation->reservation_spot = $this->getMaxValueWhereDate($reservation_datetime);
$reservation->reservation_spot++;

After this when i run my form i get error: 之后,当我运行表单时,我得到了错误:

Statement could not be executed (42000 - 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' '55', '41', '1', 0, '2015-09-08 16:23:27')' at line 1) 语句无法执行(42000-1064-您的SQL语法有错误;请查看与您的MariaDB服务器版本相对应的手册,以获取在'55','41','1',0附近使用的正确语法,'2015-09-08 16:23:27')'在第1行)

The Query from my function is 从我的函数查询是

SELECT MAX(reservation_spot) AS `maxValue` FROM `reservation` WHERE `reservation_date` = '2015-09-30 08:00'

@UPDATE Solved, edited to @UPDATE已解决,编辑为

public function getMaxValueWhereDate($reservation_datetime)
{
    $sql = $this->tableGateway->getSql();
    $select = $sql->select();
    $select->columns(array(
        'maxValue' => new Expression('MAX(reservation_spot)')
    ));
    $select->where(array('reservation_datetime' => $reservation_datetime));

    return $this->tableGateway->selectWith($select);
}

and

$reservation->reservation_spot = (int)$this->getMaxValueWhereDate($reservation_datetime);

You are returning a row result which is an instance of your model and not the value you want. 您返回的行结果是模型实例,而不是所需的

In your first example, change this line: 在第一个示例中,更改以下行:

return $row->maxValue;

You also have to to declare the maxValue field in your model or it won't be mapped. 您还必须在模型中声明maxValue字段,否则不会被映射。

Another possibility, if you don't need this field in your model, is to use an already existing field: 如果您在模型中不需要此字段,则另一种可能性是使用现有的字段:

$select->columns(array(
    'reservation_spot' => new Expression('MAX(reservation_spot)')
));

And then: 接着:

return $row->reservation_spot;
$date = '2015-09-30 8:00';
$select = $this->getSql()->select()
           ->columns(array('Reservation ' => new \Zend\Db\Sql\Expression('MAX(reservation_spot)')))
           ->where("reservation_datetime ='$date'");
        $resultSet = $this->selectWith($select);
        $row = $resultSet->current();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM