Bug in Phalcon\Mvc\Model `__set()` method for array data params
As this forum post. I think there is a bug in __set() method and the relation alias should be checked in array assignment before fall in relation logics (or just use __isset() method):
/**
* Magic method to assign values to the the model
*
* @param string property
* @param mixed value
*/
public function __set(string property, value)
{
var lowerProperty, related, modelName, manager, lowerKey, relation, referencedModel,
key, item;
/**
* Values are probably relationships if they are objects
*/
if typeof value == "object" {
if value instanceof ModelInterface {
let lowerProperty = strtolower(property),
this->{lowerProperty} = value,
this->_related[lowerProperty] = value,
this->_dirtyState = self::DIRTY_STATE_TRANSIENT;
return value;
}
}
/**
* Check if the value is an array
*/
if typeof value == "array" {
let lowerProperty = strtolower(property),
modelName = get_class(this),
manager = this->getModelsManager();
/* check relation here */
let relation = <RelationInterface> manager->getRelationByAlias(modelName, lowerProperty); // <-- LOGIC HERE
if typeof relation == "object" {
let related = [];
for key, item in value {
if typeof item == "object" {
if item instanceof ModelInterface {
let related[] = item;
}
} else {
let lowerKey = strtolower(key),
this->{lowerKey} = item,
relation = <RelationInterface> manager->getRelationByAlias(modelName, lowerProperty);
if typeof relation == "object" {
let referencedModel = manager->load(relation->getReferencedModel());
referencedModel->writeAttribute(lowerKey, item);
}
}
}
if count(related) > 0 {
let this->_related[lowerProperty] = related,
this->_dirtyState = self::DIRTY_STATE_TRANSIENT;
}
return value;
}
}
/**
* Fallback assigning the value to the instance
*/
let this->{property} = value;
return value;
}
Any idea? or workaround for this bug?
Thanks
Any comments?
Still, there is no way to assign an array to model's property!
I think this is a bug.
Thanks.
Hey,
Nothing here yet?
To assign array data to model's property?
Could you please explain a bit more use case?
@sergeyklay sure.
Consider this model:
class MyModel extends \Phalcon\Mvc\Model
{
/**
* @var array
*/
protected $my_prop;
public function setMyProp(array $my_prop){
$this->my_prop = serialize(array_filter($my_prop));
return $this;
}
public function getMyProp(){
return is_null($this->my_prop) ? [] : unserialize($this->my_prop);
}
}
Using this model as follow does not works:
$model = new MyModel();
$model->my_prop = ['Mike', 'John', 'Soheil'];
$model->save();
The my_prop does not save anymore because this falls to relation check logic.
This line
I change my Base model class with this line of code for workaround:
class Base extends \Phalcon\Mvc\Model
{
...
public function __set($property, $value)
{
if(!is_object($value)) {
if(!$this->getModelsManager()->getRelationByAlias(get_class($this), strtolower($property))) {
/**
* Check if the property has any setter
*/
if ($this->_possibleSetter($property, $value)) {
return $value;
}
}
}
return parent::__set($property, $value);
}
...
}
Thanks.
IF you created setter why you not using it ? I don't understand the problem. Just do:
$model->setMyProp(['Mike', 'John', 'Soheil']);
I don't understand why you adding setter and using property.
To be honest magic setter and getter is the worst thing that could happen to php.
@Jurigag Maybe you're right but Developers can decide which strategy is more appropriate for the application they are creating.
See this
Anyway, I think, your suggestion does not match to the issue we already have here!
Thanks.
But in docs is about PUBLIC PROPERTIES vs SETTERS/GETTERS, you created protected property and you trying to access it. You need setter for it(like in any other language like c# or java).
@Jurigag So, are you saying that the Phalcon Model's __set and _possibleSetter strategies are totally wrong decision or bad feature? Are you trying to mention that?
Yes, it's wrong decission and bad feature in my opinion, basically __set method should be never added into php. I just don't understand - if you want to set private/protected property - just add setter and use it.
@Jurigag If so, I think this is another topic that we can discuss about somewhere else.
In the current state, I think this is a bug that we can not assign and array data to protected property with setter method in our model. see this:
...
$model = new MyModel();
$model->my_prop = 'soheil'; // This works
$model->my_prop = 123; // This works too
$model->my_prop = ['Mike', 'John', 'Soheil']; // Does not work!
...
Isn't weird?
Thanks
Yes, it's a bug. But why you want to use magic setters if YOU ALREADY ADDED SETTER ?! I don't understand it, read the phalcon docs or any language docs perhaps ?
If you want to set inaacessible property - make them public or use setter. Just forget that magic setter even exists. It will make your life easier.
@Jurigag Okey, in short consider this usecase:
$model->save($_POST, ['name','my_prop']);
For record:
Actually in the above approach of saving (passing data as argument to save()), the setter will be called for array data! (Thanks to this line of code)
@sergeyklay
It will work. The problem is not with _possibleSetter but with __set method.
Im using your example myself, it doesn't using __set method.
@Jurigag What are you meaning "It will work" ?
Actually my last example is for this question:
But why you want to use magic setters if YOU ALREADY ADDED SETTER ?!
@Jurigag This is not about just using __set magic method to have magic setting stuff. If you look closer, Actually The _possibleSetter() is a handmade magic setter, logically :)
But it doesn't using magic setter in the case you wrote. It's just calling setter if there is one, if no - it's just setting value using this->{key} = value;
I'm closing this issue due to the lack of any reaction. I'll open it again if the need arises
Model
class Robot extends \Phalcon\Mvc\Model
{
public $id;
public $name;
public $status;
}
$params = [
'name' => 'aaaa',
'status' => ['a', 'b', 'c'];
];
$robot = new Robot($params);
print_r($robot->toArray());
Array
(
[id] =>
[name] => test name
[status] => Array
(
[0] => a
[1] => b
[2] => c
)
)
class Robot extends \Phalcon\Mvc\Model
{
public $id;
public $name;
}
$params = [
'name' => 'aaaa',
'status' => ['a', 'b', 'c'];
];
$robot = new Robot($params);
print_r($robot->toArray());
Array
(
[id] =>
[name] => test name
[status] =>
)
@niden Could you please take a look