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

ODM Model's protected properties aren't saved

Good Day

Have the following model

<?php

use Phalcon\Mvc\Collection;

class Customer extends Collection
{
    protected $clientId;
    protected $created;

    public function beforeCreate()
    {
        $this->created = new \DateTime();
    }

    public function getSource()
    {
        return 'customers';
    }

    public function setClientID($clientId)
    {
        $this->clientId = $clientId;

        return $this;
    }
}

$customer = new Customer()
$customer->setClientID('USER1')
    ->save();

But properies not saved, if I declare them as public it works.

What's the problem here?

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/collection.zep#L882-L905 I can clearly see it should work?

edited Jun '15

To my understanding it would ony save if you implement a getter as well as a setter

also

        /**
         * We only assign values to the public properties
         */

comes from what you linked.

I am not 100% positive this is correct, but from the behaviour you describe it seems to be.



1.4k
<?php

class Collection {
    public $a;
    protected $b;

    public function save()
    {
        print_r(get_object_vars($this));
    }
}

class Customer extends Collection {
    public $name;
    protected $email;
    protected $phone;
    private $telNo;
}

$customer  = new Customer();
$customer->save();
// output

Array
(
    [name] => 
    [email] => 
    [phone] => 
    [a] => 
    [b] => 
)
)

Hi Olegkhuss,

The problem with what you have sent is that it is just a printed array, it does not go anywhere near mongo. Also you are getting the protected properties from within the model. Try getting them from an outside model...



1.4k

Lewis, that's just to illustrate how it works, I'm not sure you understand

Hi Olegkhuss,

The problem with what you have sent is that it is just a printed array, it does not go anywhere near mongo. Also you are getting the protected properties from within the model. Try getting them from an outside model...

2 things... Firstly no need to be so rude Secondly, you have a problem using collections that I do not have!

Also you are getting the protected properties from within the model. Try getting them from an outside model...

As in not the parent model...



1.4k

Lewis, what did I say wrong?, I'm sorry. :handshakes:

https://php.net/manual/en/language.oop5.visibility.php

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

This line https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/collection.zep#L882 should return public/protected properties of my collection and it shoud be saved?

Where I'm wrong? @phalcon please?