[BUG] Setters / Getters in models must be protected...
Hi there,
I notice something in documentation about setters & getters. In documentation v4 https://docs.phalcon.io/4.0/en/db-models in setters section is mention that properties must be private but in v3 documentation is protected.
So where is the bug? If i set it private (i use phalcon v4) - dosnt matter i save or get the model object every time the getter/setter is executed.
Soo simple example (see private $password part):
<?php
namespace Models;
class Clients extends \Phalcon\Mvc\Model
{
public $id;
private $password; // here is the issue. If the property is set to protected - everything is fine
public function setPassword(string $password = null)
{
if ($password and trim($password) !== '') {
$security = $this->getDI()->getShared("security");
$this->password = $security->hash($password);
}
}
}
And... thats it. If i make property protected- everything works fine. Is it documentation mistake or i do smnth wrong? So, i am with Windows/PHP 7.3.10 / Phalcon 4.1.0
Thanks :)
Well... okay :) Lets say is documentation mistake :)
It seems that you are not declared getter. Try with private and both methods declared: getPassword() and setPassword(). Otherwise, \Phalcon\Mvc\Model won't have access to child private property to fill it with value, that's why it must be protected.
It seems that you are not declared getter. Try with private and both methods declared:
getPassword()andsetPassword(). Otherwise,\Phalcon\Mvc\Modelwon't have access to child private property to fill it with value, that's why it must be protected.
I have getter too, but did not mention in example. Same issue persist :)
Now i remember already report that issue before months, but still no response... : https://github.com/phalcon/cphalcon/issues/15199
Fast reproduce:
Model
namespace Models;
class Test extends \Phalcon\Mvc\Model
{
public $id;
private $text;
public function setText()
{
echo 'SET TEXT';
exit;
}
public function getText()
{
echo 'GET TEXT';
exit;
}
}
In controller:
$t = new \Models\Test();
$t->findFirst();
So, when is $text protected- all works fine. In example what message u get (its not GET TEXT) :)
This happens because Phalcon uses setters for populating database records into models.
When you do findFirst() there is not read/get access happens, but it writes data into the model. Thats why you see "SET TEXT".
It can be disabled globally (see doc):
ini_set('phalcon.orm.disable_assign_setters', true);
I vaguely remember something similar to this - it could be related though. There was an issue with getters and setters and also properties in Phalcon where we had to create some sort of voodoo magic to guess whether a property is a property or is coming from a magic method, or indeed it is a method call. I think that there was a quick patch for this so that it would work. This was back in the v4 alpha days. It could be related... not sure.
I am guessing that what @borisdelev is experiencing is related to the above. In theory, even if you have a private property in class Test the underlying extended Phalcon\Mvc\Model should be able to access all the methods/properties of Test. It appears that this is not the case though.
I am inclined to change the documentation to note that, instead of trying to delve deep into the Models structure to figure out why this does or does not work. Changing the property visibility should be enough.
@borisdelev can you be so kind as to point where in the docs this is located so that I can correct it?
Well... https://docs.phalcon.io/4.0/en/db-models - search about "Setters/Getters"
Thank you!
@borisdelev thank you. Corrected with https://github.com/phalcon/docs/commit/14fb4c8d2875c5404629e49a507b339213b9def7