We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Unset property from model on Phalcon 3

I have updated from PHP 5 to 7 and Phalcon 2 to 3. I am fetching a user from the databse using findFirst and immediately after than I do unset($user->userid) so the id is not shown on the result. Then I pass $user through json_encode.

This notice is thrown and this is the result of json_encode:

Access to undefined property People::userid

    "payload": {
      "id": "2",
      "userid": null,
      "name": "Test Person",
      "email": "[email protected]",

Before, with PHP 5 and Phalcon 2, userid wasn't showing up on the result.

The best way I found so far was to override this function in the model but I do not like this approach:

public function jsonSerialize()
    $array = $this->toArray();
    return $array;

$user->userid = null; 

perhaps ?

That's not a solution if I want to exclude userid entirely from the JSON response.