doupeng8419 2015-09-05 16:42
浏览 39
已采纳

Zend Framework 2 TableGateway选择错误

I want to know how many reservations are made at $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

The line 99 is:

$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;
    }    
}
  • 写回答

1条回答 默认 最新

  • donglu6303 2015-09-06 08:15
    关注

    You have forgot to include the Select class from Zend. 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;
        }    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化