{"action":"closed","issue":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/8686","repository_url":"https://api.github.com/repos/cakephp/cakephp","labels_url":"https://api.github.com/repos/cakephp/cakephp/issues/8686/labels{/name}","comments_url":"https://api.github.com/repos/cakephp/cakephp/issues/8686/comments","events_url":"https://api.github.com/repos/cakephp/cakephp/issues/8686/events","html_url":"https://github.com/cakephp/cakephp/issues/8686","id":149994501,"number":8686,"title":"Add a method to validate count of HABTM and hasMany assocs","user":{"login":"burzum","id":162789,"avatar_url":"https://avatars.githubusercontent.com/u/162789?v=3","gravatar_id":"","url":"https://api.github.com/users/burzum","html_url":"https://github.com/burzum","followers_url":"https://api.github.com/users/burzum/followers","following_url":"https://api.github.com/users/burzum/following{/other_user}","gists_url":"https://api.github.com/users/burzum/gists{/gist_id}","starred_url":"https://api.github.com/users/burzum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/burzum/subscriptions","organizations_url":"https://api.github.com/users/burzum/orgs","repos_url":"https://api.github.com/users/burzum/repos","events_url":"https://api.github.com/users/burzum/events{/privacy}","received_events_url":"https://api.github.com/users/burzum/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/cakephp/cakephp/labels/RFC","name":"RFC","color":"c7def8"},{"url":"https://api.github.com/repos/cakephp/cakephp/labels/validation","name":"validation","color":"FFFFFF"}],"state":"closed","locked":false,"assignee":{"login":"burzum","id":162789,"avatar_url":"https://avatars.githubusercontent.com/u/162789?v=3","gravatar_id":"","url":"https://api.github.com/users/burzum","html_url":"https://github.com/burzum","followers_url":"https://api.github.com/users/burzum/followers","following_url":"https://api.github.com/users/burzum/following{/other_user}","gists_url":"https://api.github.com/users/burzum/gists{/gist_id}","starred_url":"https://api.github.com/users/burzum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/burzum/subscriptions","organizations_url":"https://api.github.com/users/burzum/orgs","repos_url":"https://api.github.com/users/burzum/repos","events_url":"https://api.github.com/users/burzum/events{/privacy}","received_events_url":"https://api.github.com/users/burzum/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/cakephp/cakephp/milestones/69","html_url":"https://github.com/cakephp/cakephp/milestones/3.3.0","labels_url":"https://api.github.com/repos/cakephp/cakephp/milestones/69/labels","id":1443324,"number":69,"title":"3.3.0","description":"A backwards compatible feature release for 3.x. See the [3.3 Roadmap](https://github.com/cakephp/cakephp/wiki/3.3-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":74,"closed_issues":57,"state":"open","created_at":"2015-12-04T02:24:56Z","updated_at":"2016-04-25T11:05:13Z","due_on":"2016-06-12T04:00:00Z","closed_at":null},"comments":0,"created_at":"2016-04-21T07:48:22Z","updated_at":"2016-04-25T11:05:13Z","closed_at":"2016-04-25T11:05:13Z","body":"This is a (multiple allowed):\r\n* [ ] bug\r\n* [x] enhancement\r\n* [x] feature-discussion (RFC)\r\n\r\n### What you did\r\nWanted to validate HABTM / hasMany for \"hast at least one item associated\"\r\n\r\n### Expected Behavior\r\nThought the core has something for this but meh... \r\n\r\nSo I ended up writing a small method and put it into a trait.\r\n\r\n```php\r\n\tpublic function validateAtLeastOne($value) {\r\n\t\tif (!is_array($value)) {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\tif (isset($value['_ids'])) {\r\n\t\t\tif (!is_array($value['_ids'])) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\treturn (count($value['_ids']) > 0);\r\n\t\t}\r\n\t\treturn (count($value) > 0);\r\n\t}\r\n```\r\n\r\nWe could implement \"hasAtLeastOne\" and \"hasAtLeastN\" or \"hasAtLeast\", the first just being a convenience method.\r\n\r\nI'm going to create a PR for this as soon we decided if and how how to implement this in the core."}}