【发布时间】:2018-04-17 14:39:37
【问题描述】:
自从我从 slim-api-skeleton 开始使用 Zend TableGateway 学习 Slim 已经有几周了。
我似乎无法使用 TableGateway 运行 2 个连续查询。它总是产生(不仅仅是更新):
"Statement couldn't be produced with sql: UPDATE `users` SET `last_access` = NOW() WHERE `id` = ?"
这是 ZendUserRepository 类中的代码:
public function __construct(array $config) {
$adapter = new Adapter($config);
$this->table = new TableGateway("users", $adapter);
}
...
public function footprint(int $id): void {
$data = ['last_access' => new Predicate\Expression('NOW()')];
$where = ['id' => $id];
$this->table->update($data, $where);
}
public function authenticate(string $username, string $password): bool {
$where = [
'username' => $username,
new Predicate\IsNotNull('roles')
];
$rowset = $this->table->select($where);
if (null === $row = $rowset->current()) {
return false;
}
$data = (array) $row;
if(password_verify($password, $data['password'])) {
$this->footprint($data['id']);
return true;
}
return false;
}
这让我沮丧了好几天。由于更新函数也使用2个连续查询。
public function update(User $user): void {
$data = $this->hydrator->extract($user);
if (!$this->contains($user)) {
throw new UserNotFoundException;
}
$where = ["id" => $user->getId()];
$this->table->update($data, $where);
}
public function contains(User $user): bool {
try {
$this->get($user->getId());
} catch (UserNotFoundException $exception) {
return false;
}
return true;
}
谢谢。
【问题讨论】:
标签: zend-framework slim