We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model beforeSave() and afterSave() not executing before WHERE statement is created in the UPDATE SQL?

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:

edited Mar '17

Hi, could you tell me from what program you have generated this SQL logs?

These are the "general" MySQL logs.

If you are running MySQL >= 5.6 add the following to your my.ini and restart the service:


# The MySQL server
[mysqld]
general_log = on
general_log_file=general.log

Hi, could you tell me from what program you have generated this SQL logs?

Big thanks

These are the "general" MySQL logs.

If you are running MySQL >= 5.6 add the following to your my.ini and restart the service:


# The MySQL server
[mysqld]
general_log = on
general_log_file=general.log

Hi, could you tell me from what program you have generated this SQL logs?