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

Save protected values in MongoDb

I am trying to use a Collection model to create a new record in Mongo. I have defined getters/setters and as well an alternative colletion name to be used (getSource()). However upon save() I get no errors but a blank document save for the _id being set.

    namespace Apps\Document\Model;
    use \Phalcon\Mvc\Collection;
    class Client extends Collection
    {
        protected $client_id;
        protected $client_secret;

        public function getSource()
        {
            return "app_clients";
        }

        public function setClientId($client_id)
        {       
            $this->client_id = $client_id;
        }   
        public function getClient_Id()
        {
            return $this->client_id;
        }
        public function setClientSecret($client_secret)
        {
            $this->client_secret = $client_secret;
        }
        public function getClientSecret()
        {
            return $this->client_secret;
        }
    }

    $AppDoc = new \Apps\Document\Model\Client();
    $AppDoc->setClientId('some id');
    $AppDoc->setClientSecret('shhhh');
    $AppDoc->save();

This results in the following document

{ "_id" : ObjectId("539ab922279871b7238b4568") }

It looks like Phalcon only save public values. Is there a way I can tell Phalcon to save protected values? I've found the 'getReservedAttributes' method but i need the opposit to tell Phalcon wich items must be saved

EDIT: 2015-03-13 A look at the C source code point me to this line:

1469: PHALCON_CALL_FUNCTION(&properties, "get_object_vars", this_ptr); https://github.com/qrazi/cphalcon/blob/master/ext/mvc/collection.c#L1469

I'm not a C programmer but i think the problem is there. The function get_object_vars only returns public properties when called form outside the object.

Is this a bug?



1.4k

Hi! I have same issue here https://forum.phalcon.io/discussion/7125/odm-models-protected-properties-arent-saved

get_object_vars will return you protected and public properties if called inside a class, so it should work, but it doesn't

I have same problem :( How to fix it? @phalcon