entity->getValueMap() is not a direct replacement for toArray()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bandtank
    Active Community Member
    • Mar 2017
    • 384

    entity->getValueMap() is not a direct replacement for toArray()

    Can someone explain why toArray has been replaced by getValueMap:
    Code:
        public function getValueMap(): stdClass
        {
            $array = $this->toArray();
    
            return (object) $array;
        }
    This is basically the same thing, except the object is harder to use than the array. I am having to modify hundreds of lines of code in my extensions and customizations to deal with this, and I am doing the following to make it work:
    Code:
    $arr = (array)$entity->getValueMap();
    I don't see how this is an improvement, and it is causing a ton of work for me. I know the getValueMap function has been around for a long time, but I never switched because it runs much more slowly than simply using toArray.
  • yuri
    Member
    • Mar 2014
    • 8574

    #2
    It has been deprecated for more than 5 years. The method was set to be removed in v9.0. IDEs strike through deprecated methods warning developers not to use it. It's also possible to use search for all deprecation usages to fix code.

    I never use toArray nor getValueMap in code. I encourage not to use associative arrays as value containers in business logic and prefer type-safe code.

    The only use of getValueMap is in the very end of API actions to send entity data to output or before storing/serializing entity data. As the array type is a woe in PHP (it's both a map and a list), the object map is preferable, because it's explicitly a map. An empty map will be encoded as a JSON object not as an empty array what is the source of bugs which usually are caught late on production.
    Last edited by yuri; Yesterday, 10:03 PM.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    Working...