since few days i have a problem with mysql query using tablegateway and zf2 functions.
I would like to get max value of column 'reservation_spot'
where 'reservation_datetime' = &reservation_datetime
The query shoud be like this
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)
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
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.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.