2015-09-05 61 views
0

我想知道$reservation_datetime有多少預訂。我寫了這個代碼,但是我有錯誤Zend Framework 2 TableGateway Select 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

線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

你已經忘了,包括從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; 
    }  
}