We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 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();
    unset($array['userid']);
    return $array;
}


85.5k
$user->userid = null; 

perhaps ?

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