{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/db/issues/12","repository_url":"https://api.github.com/repos/yiisoft/db","labels_url":"https://api.github.com/repos/yiisoft/db/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/db/issues/12/comments","events_url":"https://api.github.com/repos/yiisoft/db/issues/12/events","html_url":"https://github.com/yiisoft/db/issues/12","id":374731956,"node_id":"MDU6SXNzdWUzNzQ3MzE5NTY=","number":12,"title":"Static relations","user":{"login":"SamMousa","id":547021,"node_id":"MDQ6VXNlcjU0NzAyMQ==","avatar_url":"https://avatars1.githubusercontent.com/u/547021?v=4","gravatar_id":"","url":"https://api.github.com/users/SamMousa","html_url":"https://github.com/SamMousa","followers_url":"https://api.github.com/users/SamMousa/followers","following_url":"https://api.github.com/users/SamMousa/following{/other_user}","gists_url":"https://api.github.com/users/SamMousa/gists{/gist_id}","starred_url":"https://api.github.com/users/SamMousa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SamMousa/subscriptions","organizations_url":"https://api.github.com/users/SamMousa/orgs","repos_url":"https://api.github.com/users/SamMousa/repos","events_url":"https://api.github.com/users/SamMousa/events{/privacy}","received_events_url":"https://api.github.com/users/SamMousa/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-10-28T08:54:11Z","updated_at":"2018-10-28T14:57:32Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Currently relationship definitions in AR are defined as non static getters. In reality an AR class represents a table and an instance of that class represents a record.\n\nRelations are defined at the table level, but because AR uses non-static getters our query classes need to instantiate a dummy model for greedy queries.\n\nRequirements for AR relationships:\n- One definition for lazy and greedy loading\n- Support (cached) retrieval via magic getter\n- Support getting a query object (currently via the getter that defines it)\n\nPros of current approach:\n- Uses \"native\" magic getters (with a small extension)\n- Has code completion for the getters since they are normal functions\n\nCons:\n- No way to get the relationships without instantiating a dummy model\n- No way to enumerate relations\n\n\nIn Yii1 we had a single function defining the relationships but we also had the dummy model due to lack of late static binding. This allowed for enumeration but had other down sides.\n\nWhat if, in Yii3 we try to get the best of both? Suppose we define relationships as static functions:\n```php\npublic static function relatedCustomers()\n{\n    return Has::many (Customer::class, ['id' => 'customer_id']);\n}\n```\n\nInternally we would have a sound definition of the relationship, from this we can:\n- build a lazy query\n- build a greedy query\n- enumerate via reflection\n\nFrom a consumer point of view nothing changes:\n- `getCustomers` via `__invoke()`\n- `->customers ` via `__get()`\n\nPros:\n- Relations are now static\n- No need for dummy instances\n- OO definition of relationship unrelated to the query object\n\n\nCons:\n- no auto complete for the getter, so could require additional annotations.\n- breaks BC"},"comment":{"url":"https://api.github.com/repos/yiisoft/db/issues/comments/433712990","html_url":"https://github.com/yiisoft/db/issues/12#issuecomment-433712990","issue_url":"https://api.github.com/repos/yiisoft/db/issues/12","id":433712990,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMzcxMjk5MA==","user":{"login":"samdark","id":47294,"node_id":"MDQ6VXNlcjQ3Mjk0","avatar_url":"https://avatars2.githubusercontent.com/u/47294?v=4","gravatar_id":"","url":"https://api.github.com/users/samdark","html_url":"https://github.com/samdark","followers_url":"https://api.github.com/users/samdark/followers","following_url":"https://api.github.com/users/samdark/following{/other_user}","gists_url":"https://api.github.com/users/samdark/gists{/gist_id}","starred_url":"https://api.github.com/users/samdark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samdark/subscriptions","organizations_url":"https://api.github.com/users/samdark/orgs","repos_url":"https://api.github.com/users/samdark/repos","events_url":"https://api.github.com/users/samdark/events{/privacy}","received_events_url":"https://api.github.com/users/samdark/received_events","type":"User","site_admin":false},"created_at":"2018-10-28T14:57:32Z","updated_at":"2018-10-28T14:57:32Z","author_association":"MEMBER","body":"1. Breaking BC is OK if there are pros and no other cons.\r\n2. I don't like that `getCustomers()` and `->customers` are currently different. That isn't explicit.\r\n3. Ability to enumerate relations is a good addition. It was asked multiple times to have it.\r\n"}}
{"actor":{"display_login":"samdark"}}