Looks like beforeSave() is not executed before the WHERE statement is created in the UPDATE...WHERE statement.
Here's my PlayerSessions model:
class PlayerSessions extends AbstractPlayerSessions
{
public function beforeSave()
{
// Convert the GUID string into binary
$this->SessionId = hex2bin(str_replace('-', '', $this->SessionId));
$this->PlayerId = hex2bin(str_replace('-', '', $this->PlayerId));
$this->RemoteIp = IP::create($this->RemoteIp)->binary();
}
public function afterSave()
{
// Convert the binary GUID to a GUID string
$this->SessionId = Uuid::fromBytes($this->SessionId)->toString();
$this->PlayerId = Uuid::fromBytes($this->PlayerId)->toString();
$this->RemoteIp = IP::create($this->RemoteIp)->humanReadable(true);
}
public function afterFetch()
{
// Convert the binary GUID to a GUID string
$this->SessionId = Uuid::fromBytes($this->SessionId)->toString();
$this->PlayerId = Uuid::fromBytes($this->PlayerId)->toString();
$this->RemoteIp = IP::create($this->RemoteIp)->humanReadable(true);
}
}
Here's how I'm using the model for INSERT/UPDATE:
$playerSession = PlayerSessions::findFirst([
'conditions' => 'PlayerId = ?1',
'bind' => [
1 => Uuid::fromString($player->getId())->getBytes()
]
]);
if(!$playerSession)
{
$playerSession = new PlayerSessions([
'PlayerId' => $player->getId()
]);
}
$sessionExpiration = new \DateTime($this->di->getShared('config')->application->sessionDuration);
$playerSession
->setSessionId(Uuid::uuid4()->toString())
->setExpiration($sessionExpiration->format('Y-m-d H:i:s'))
->setCreated((new \DateTime())->format('Y-m-d H:i:s'))
->setRemoteIp($this->request->getClientAddress());
if(!$playerSession->save())
{
$this->db->rollback();
$validationFault = new ValidationFaultResponse(HttpStatusCode::BadRequest);
$validationFault->addPhalconModelMessages($playerSession->getMessages());
return $this->response
->setStatusCode(HttpStatusCode::BadRequest)
->setJsonContent($validationFault);
}
$this->db->commit();
The INSERT statement is working perfectly, but the UPDATE statement isn't. I expect the UPDATE statement to have the PlayerId set to the binary value of 'A62453FF998346448870CAE768FE9001' (because of my model's beforeSave() event).
Here's an image of the INSERT and UPDATE statements generated by Phalcon: