{"action":"created","issue":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/9474","repository_url":"https://api.github.com/repos/cakephp/cakephp","labels_url":"https://api.github.com/repos/cakephp/cakephp/issues/9474/labels{/name}","comments_url":"https://api.github.com/repos/cakephp/cakephp/issues/9474/comments","events_url":"https://api.github.com/repos/cakephp/cakephp/issues/9474/events","html_url":"https://github.com/cakephp/cakephp/issues/9474","id":177593773,"number":9474,"title":"(Automatically) Convert empty association properties for `SAVE_REPLACE` compatibility?","user":{"login":"ndm2","id":5031606,"avatar_url":"https://avatars.githubusercontent.com/u/5031606?v=3","gravatar_id":"","url":"https://api.github.com/users/ndm2","html_url":"https://github.com/ndm2","followers_url":"https://api.github.com/users/ndm2/followers","following_url":"https://api.github.com/users/ndm2/following{/other_user}","gists_url":"https://api.github.com/users/ndm2/gists{/gist_id}","starred_url":"https://api.github.com/users/ndm2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ndm2/subscriptions","organizations_url":"https://api.github.com/users/ndm2/orgs","repos_url":"https://api.github.com/users/ndm2/repos","events_url":"https://api.github.com/users/ndm2/events{/privacy}","received_events_url":"https://api.github.com/users/ndm2/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/cakephp/cakephp/labels/Enhancement","name":"Enhancement","color":"bfe5bf"},{"url":"https://api.github.com/repos/cakephp/cakephp/labels/ORM","name":"ORM","color":"ffffff"},{"url":"https://api.github.com/repos/cakephp/cakephp/labels/RFC","name":"RFC","color":"c7def8"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/cakephp/cakephp/milestones/95","html_url":"https://github.com/cakephp/cakephp/milestones/3.4.0","labels_url":"https://api.github.com/repos/cakephp/cakephp/milestones/95/labels","id":1906253,"number":95,"title":"3.4.0","description":"Backwards compatible features for 3.x. See the [3.4-Roadmap](https://github.com/cakephp/cakephp/wiki/3.4-Roadmap).","creator":{"login":"markstory","id":24086,"avatar_url":"https://avatars.githubusercontent.com/u/24086?v=3","gravatar_id":"","url":"https://api.github.com/users/markstory","html_url":"https://github.com/markstory","followers_url":"https://api.github.com/users/markstory/followers","following_url":"https://api.github.com/users/markstory/following{/other_user}","gists_url":"https://api.github.com/users/markstory/gists{/gist_id}","starred_url":"https://api.github.com/users/markstory/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markstory/subscriptions","organizations_url":"https://api.github.com/users/markstory/orgs","repos_url":"https://api.github.com/users/markstory/repos","events_url":"https://api.github.com/users/markstory/events{/privacy}","received_events_url":"https://api.github.com/users/markstory/received_events","type":"User","site_admin":false},"open_issues":79,"closed_issues":33,"state":"open","created_at":"2016-07-27T02:20:40Z","updated_at":"2016-09-17T23:35:22Z","due_on":"2016-12-18T00:00:00Z","closed_at":null},"comments":8,"created_at":"2016-09-17T17:38:00Z","updated_at":"2016-09-19T09:51:30Z","closed_at":null,"body":"This is a (multiple allowed):\r\n* [ ] bug\r\n* [x] enhancement\r\n* [x] feature-discussion (RFC)\r\n\r\n---\r\n\r\nLet's say we have a form that supports adding/removing `hasMany`/`belongsToMany` associated records that are using the `SAVE_REPLACE` save strategy.\r\n\r\n```php\r\n$this->Form->input('parent_field');\r\n$this->Form->input('association.0.field');\r\n$this->Form->input('association.1.field');\r\n$this->Form->input('association.2.field');\r\n// ...\r\n```\r\n\r\nWhen removing for example `association.2.field`, the saving process would delete the record accordingly. When however removing all of them, so that we'd be left with only\r\n\r\n```php\r\n$this->Form->input('parent_field');\r\n```\r\n\r\nthen the association property wouldn't be affected anymore at all, and nothing would change, ie the records wouldn't be deleted.\r\n\r\nTo workaround that one have to do something like adding a hidden field with the name of the association property, and check that before marshalling and replace it with an empty array accordingly, like for example\r\n\r\n```php\r\n$this->Form->input('parent_field');\r\n$this->Form->hidden('association', ['value' => '']);\r\n$this->Form->input('association.0.field');\r\n$this->Form->input('association.1.field');\r\n$this->Form->input('association.2.field');\r\n// ...\r\n```\r\n\r\n```php\r\npublic function beforeMarshal(Event $event, \\ArrayObject $data, \\ArrayObject $options)\r\n{\r\n\tif (isset($data['association']) && $data['association'] === '') {\r\n\t\t$data['association'] = [];\r\n\t}\r\n}\r\n```\r\n\r\nThat way all records would be deleted properly when none of the `association.*.field` inputs would be submitted.\r\n\r\nI'm wondering if this is something that may fit in the core? Maybe as an optional behavior that can be enabled via the `associated` option?\r\n"},"comment":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/comments/247952117","html_url":"https://github.com/cakephp/cakephp/issues/9474#issuecomment-247952117","issue_url":"https://api.github.com/repos/cakephp/cakephp/issues/9474","id":247952117,"user":{"login":"inoas","id":20972207,"avatar_url":"https://avatars.githubusercontent.com/u/20972207?v=3","gravatar_id":"","url":"https://api.github.com/users/inoas","html_url":"https://github.com/inoas","followers_url":"https://api.github.com/users/inoas/followers","following_url":"https://api.github.com/users/inoas/following{/other_user}","gists_url":"https://api.github.com/users/inoas/gists{/gist_id}","starred_url":"https://api.github.com/users/inoas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inoas/subscriptions","organizations_url":"https://api.github.com/users/inoas/orgs","repos_url":"https://api.github.com/users/inoas/repos","events_url":"https://api.github.com/users/inoas/events{/privacy}","received_events_url":"https://api.github.com/users/inoas/received_events","type":"User","site_admin":false},"created_at":"2016-09-19T09:51:30Z","updated_at":"2016-09-19T09:51:30Z","body":"Can't this be fixed at the controller/table level with an option flag?"}}
{"actor":{"display_login":"inoas"}}