简体   繁体   English

Zend Framework 2 TableGateway选择错误

[英]Zend Framework 2 TableGateway Select error

I want to know how many reservations are made at $reservation_datetime . 我想知道在$reservation_datetime进行了多少次$reservation_datetime I wrote this code however I have error 我写了这段代码但是有错误

Catchable fatal error: Argument 1 passed to Reservations\\Model\\ReservationTable::Reservations\\Model{closure}() must be an instance of Reservations\\Model\\Select, instance of Zend\\Db\\Sql\\Select given, called in /home/.../vendor/zendframework/zend-db/src/TableGateway/AbstractTableGateway.php on line 190 and defined in /home/.../module/Reservations/src/Reservations/Model/ReservationTable.php on line 99 可捕获的致命错误:传递给Reservations \\ Model \\ ReservationTable :: Reservations \\ Model {closure}()的参数1必须是Reservations \\ Model \\ Select的实例,Zend \\ Db \\ Sql \\ Select的实例已给定,在/ home /中调用... /第190行的/vendor/zendframework/zend-db/src/TableGateway/AbstractTableGateway.php并在第99行的/home/.../module/Reservations/src/Reservations/Model/ReservationTable.php中定义

The line 99 is: 第99行是:

$this->num = $this->tableGateway->select(function (Select $select) {
        $select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
        $select->where(array('reservation_date' => $reservation_datetime));
    });

Of this code 这段代码

class ReservationTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function saveReservation(Reservation $reservation)
    {
        $data = array(
            'reservation_date' => $reservation_datetime,
            ...
        );

        $reservation_spot = (int)1;
        $rsn = $this->getReservationCount($reservation_datetime);
        if($rsn < 4) {
            for($i=1; $i<4; $i++){
                if($rsn = $i) {
                    $reservation_spot++;
                }
            }
        }
        else {
            throw new \Exception('No available spots');
        }

    }

    public function getReservationCount($reservation_datetime)
    {
        $this->num = $this->tableGateway->select(function (Select $select) {
            $select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
            $select->where(array('reservation_date' => $reservation_datetime));
        });

        return $this->num;
    }    
}

You have forgot to include the Select class from Zend. 您忘记了包含Zend中的Select类。 At the top of the class add this line. 在课程的顶部添加此行。

use Zend\\Db\\Sql\\Select;

use Zend\Db\Sql\Select;

class ReservationTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function saveReservation(Reservation $reservation)
    {
        $data = array(
            'reservation_date' => $reservation_datetime,
            ...
        );

        $reservation_spot = (int)1;
        $rsn = $this->getReservationCount($reservation_datetime);
        if($rsn < 4) {
            for($i=1; $i<4; $i++){
                if($rsn = $i) {
                    $reservation_spot++;
                }
            }
        }
        else {
            throw new \Exception('No available spots');
        }

    }

    public function getReservationCount($reservation_datetime)
    {
        $this->num = $this->tableGateway->select(function (Select $select) {
            $select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
            $select->where(array('reservation_date' => $reservation_datetime));
        });

        return $this->num;
    }    
}

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

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