doupeng8419 2015-09-05 16:42 采纳率: 0%
浏览 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;
        }    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥50 WPF Lidgren.Network.Core2连接问题
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建