{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/9355","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/9355/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/9355/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/9355/events","html_url":"https://github.com/yiisoft/yii2/issues/9355","id":99795864,"number":9355,"title":"BaseActiveRecord could do with it's own setAttributes()","user":{"login":"Sammaye","id":323996,"avatar_url":"https://avatars.githubusercontent.com/u/323996?v=3","gravatar_id":"","url":"https://api.github.com/users/Sammaye","html_url":"https://github.com/Sammaye","followers_url":"https://api.github.com/users/Sammaye/followers","following_url":"https://api.github.com/users/Sammaye/following{/other_user}","gists_url":"https://api.github.com/users/Sammaye/gists{/gist_id}","starred_url":"https://api.github.com/users/Sammaye/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sammaye/subscriptions","organizations_url":"https://api.github.com/users/Sammaye/orgs","repos_url":"https://api.github.com/users/Sammaye/repos","events_url":"https://api.github.com/users/Sammaye/events{/privacy}","received_events_url":"https://api.github.com/users/Sammaye/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-08-08T11:32:48Z","updated_at":"2015-08-08T15:14:59Z","closed_at":null,"body":"I came across this when needed to implement my own `populateRecord` due to finding that console edited documents in my MongoDB totally mess up PHP data types.\r\n\r\nEssentially the problem is in performance. You use the `Model` function which looks like:\r\n```php\r\n    public function setAttributes($values, $safeOnly = true)\r\n    {\r\n        if (is_array($values)) {\r\n            $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());\r\n            foreach ($values as $name => $value) {\r\n                if (isset($attributes[$name])) {\r\n                    $this->$name = $value;\r\n                } elseif ($safeOnly) {\r\n                    $this->onUnsafeAttribute($name, $value);\r\n                }\r\n            }\r\n        }\r\n    }\r\n```\r\n\r\nWhich looks fine until you realise what `__set` does in `BaseActiveRecord` when calling `hasAttribute`:\r\n```php\r\n    public function hasAttribute($name)\r\n    {\r\n        return isset($this->_attributes[$name]) || in_array($name, $this->attributes());\r\n    }\r\n```\r\n\r\nAs you can see it is doubling (at least) the work of the function by calling `$this->$name`. Instead `BaseActiveRecord` should have it's own `setAttributes` which looks like:\r\n\r\n```php\r\n    public function setAttributes($values, $safeOnly = true)\r\n    {\r\n        if (is_array($values)) {\r\n            $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());\r\n            foreach ($values as $name => $value) {\r\n                if (isset($attributes[$name])) {\r\n                    $this->_attributes[$name] = $value;\r\n                } elseif ($safeOnly) {\r\n                    $this->onUnsafeAttribute($name, $value);\r\n                }\r\n            }\r\n        }\r\n    }\r\n```\r\n\r\nThat would make that function a lot more performant by the looks of it.\r\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/128989320","html_url":"https://github.com/yiisoft/yii2/issues/9355#issuecomment-128989320","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/9355","id":128989320,"user":{"login":"Sammaye","id":323996,"avatar_url":"https://avatars.githubusercontent.com/u/323996?v=3","gravatar_id":"","url":"https://api.github.com/users/Sammaye","html_url":"https://github.com/Sammaye","followers_url":"https://api.github.com/users/Sammaye/followers","following_url":"https://api.github.com/users/Sammaye/following{/other_user}","gists_url":"https://api.github.com/users/Sammaye/gists{/gist_id}","starred_url":"https://api.github.com/users/Sammaye/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sammaye/subscriptions","organizations_url":"https://api.github.com/users/Sammaye/orgs","repos_url":"https://api.github.com/users/Sammaye/repos","events_url":"https://api.github.com/users/Sammaye/events{/privacy}","received_events_url":"https://api.github.com/users/Sammaye/received_events","type":"User","site_admin":false},"created_at":"2015-08-08T15:14:59Z","updated_at":"2015-08-08T15:14:59Z","body":"Well my sight of the performance problem is in relation to alsp not considering the parent __set. You see I was seeing from the `populateRecord` function that I was trying to manipulate in `BaseActiveRecord`.\r\n\r\nAs I tried to fix the performance in this function, in my own `BaseActiveRecord` child, I realised that `setAttributes` would be great to use if it had that fix done. Of course, I did not foresee that the `parent::__set` would not be called for other scenarios.\r\n\r\nSo that gives you direction about my perspective. That is why I gave off a frank reply that really a way to override the `hasAttribute` method and get around it, like Yii2 does in `populateRecord`, without being in the same class would be the most ideal solution for me.\r\n\r\n> Why dont you implement it in you'r own function as in: _attributes_dynamic\r\n\r\nYou mean as a function in my own child `BaseActiveRecord`? Because 80% of the `BaseActiveRecord` class uses `_attributes` in some way. PHP's private scoping would require me to copy the entire class nearly to do it. This means bad code reuse, especially for potential Yii2 updates.\r\n\r\n"}}