Zend Framework 2 TableGateway选择错误

时间:2015-09-05 16:42:18

标签: php zend-framework

我想知道$reservation_datetime有多少预订。我写了这段代码,但是我有错误

  

捕获致命错误:参数1传递给Reservations \ Model \ ReservationTable :: Reservations \ Model {closure}()必须是Reservations \ Model \ Select的实例,Zend \ Db \ Sql \ Select的实例给定,调用/home/.../vendor/zendframework/zend-db/src/TableGateway/AbstractTableGateway.php在第190行并在/home/.../module/Reservations/src/Reservations/Model/ReservationTable.php中定义99

第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));
    });

此代码

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;
    }    
}

1 个答案:

答案 0 :(得分:1)

您忘记包含Zend的Select课程。在课程的顶部添加这一行。

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;
    }    
}