cphalcon/phalcon/mvc/model.zep
Why not add function:
protected final function _possibleGetter(string property)
{
var possibleGetter;
let possibleGetter = "get" . camelize(property);
if method_exists(this, possibleGetter) {
return this->{possibleGetter}();
}
return this->{attributeField};
}
then, called in the toArray():
/**
* Returns the instance as an array representation
*
*<code>
* print_r(
* $robot->toArray()
* );
*</code>
*
* @param array $columns
* @return array
*/
public function toArray(columns = null) -> array
{
......
for attribute in metaData->getAttributes(this) {
....
let data[attributeField] = this->_possibleGetter(attributeField);
}
return data;
}
instead of :
/**
* Returns the instance as an array representation
*
*<code>
* print_r(
* $robot->toArray()
* );
*</code>
*
* @param array $columns
* @return array
*/
public function toArray(columns = null) -> array
{
......
for attribute in metaData->getAttributes(this) {
....
if fetch value, this->{attributeField} {
let data[attributeField] = value;
} else {
let data[attributeField] = null;
}
}
return data;
}
The question is - why? Method call is heavy and since we are already in model scope why just not use properties?
@Jurigag cause u can have transformations in get like json_decode, serialize, implode, explode, etc...
Getter is not a place for this. Use events.
@Jurigag sure? Update with assign array consider use of set methods so as the same way toArray could be consider get methods. why not reflect same effects?
@Jurigag if the model properties are "protected" , and do some thing in getter function ; the result of property not same as "toArray()" return
But getter is getter, it's not for operating over properties. Getter should only just return value of protected property. Nothing more. If you put there anything more then it's wrong.
Hi,
I always prefer to use PHP鈥檚 DateTime instead of some string that can be considered a date... I always use getters and setters that transform from and to DateTime objects.
Just a comment on why a toArray could use getters. Maybe you can add this as a configurable feature, disabled by default for performance?
Regards,
Federico.
We need just to use our brains.
If i use get methods, why the fuck toArray() should ignore them? nevermind, just think!
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues
Most helpful comment
@Jurigag sure? Update with assign array consider use of set methods so as the same way toArray could be consider get methods. why not reflect same effects?