{"id":1589,"date":"2023-05-25T23:13:10","date_gmt":"2023-05-25T14:13:10","guid":{"rendered":"https:\/\/blog.fourthgate.jp\/?p=1589"},"modified":"2023-06-14T20:53:23","modified_gmt":"2023-06-14T11:53:23","slug":"cakephp4%e3%81%a7hasmany%e3%81%95%e3%82%8c%e3%81%9f%e3%83%ac%e3%82%b3%e3%83%bc%e3%83%89%e3%81%ae%e3%82%ab%e3%82%a6%e3%83%b3%e3%83%88","status":"publish","type":"post","link":"https:\/\/blog.fourthgate.jp\/?p=1589","title":{"rendered":"CakePHP4\u3067HasMany\u3055\u308c\u305f\u30ec\u30b3\u30fc\u30c9\u306e\u30ab\u30a6\u30f3\u30c8"},"content":{"rendered":"<p>\u50d5\u306f\u300c\u535a\u591a\u5f01\u3067\u3044\u3053\u3046\uff01\u300d\u3068\u3044\u3046\u30b5\u30a4\u30c8\u3092\u904b\u7528\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3053\u306e\u30b5\u30a4\u30c8\u306f\u3001\u3082\u304625\u5e74\u304f\u3089\u3044\u524d\u306bPerl\u306ecgi\u3067\u4f5c\u3063\u305f\u306e\u304c\u6700\u521d\u3067\u3001\u305d\u306e\u5f8c\u3001PHP\u3078\u79fb\u690d\u3002<br \/>\n\u3055\u3089\u306b\u3001CakePHP3\u5316\u3057\u3001\u73fe\u5728\u306fCakePHP4\u3067\u904b\u7528\u3057\u3066\u3044\u307e\u3059\u3002CakePHP\u5316\u3057\u305f\u306e\u306f\u3001\u52c9\u5f37\u306e\u305f\u3081\u3068\u3044\u3046\u610f\u5473\u5408\u3044\u304c\u5f37\u3044\u306e\u3067\u3059\u304c\u3002<\/p>\n<p>\u3053\u306e\u30b5\u30a4\u30c8\u306b\u306f\u3001\u63b2\u8f09\u3057\u3066\u3044\u308b\u8a00\u8449\uff08\u535a\u591a\u5f01\uff09\u3092\u4f7f\u3063\u3066\u3044\u305f\u304b\u3069\u3046\u304b\u306e\u30a2\u30f3\u30b1\u30fc\u30c8\u304c\u3042\u308a\u307e\u3059\u3002\u30a2\u30f3\u30b1\u30fc\u30c8\u306e\u5185\u5bb9\u306f\u3001\u4f7f\u3063\u3066\u3044\u305f\u304b\u3069\u3046\u304b\u306e\u9078\u629e\u3068\u3001\u56de\u7b54\u8005\u306e\u751f\u5e74\u3068\u6027\u5225\u3001\u305d\u308c\u3068\u30d5\u30ea\u30fc\u306e\u30b3\u30e1\u30f3\u30c8\u304b\u3089\u306a\u308a\u307e\u3059\u3002<br \/>\n\u5404\u8a00\u8449\u306f\u30c6\u30fc\u30d6\u30eb\uff08words\uff09\u306b\u3001\u30a2\u30f3\u30b1\u30fc\u30c8\u7d50\u679c\u306f\u30c6\u30fc\u30d6\u30eb\uff08votes\uff09\u306b\u4fdd\u5b58\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u30c6\u30fc\u30d6\u30eb\uff08words\uff09\u306f\u3001\u30c6\u30fc\u30d6\u30eb\uff08votes\uff09\u3092HasMany\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6295\u7968\u7d50\u679c\u306e\u30da\u30fc\u30b8\u3067\u306f\u3001\u4f7f\u3063\u3066\u3044\u305f\u304b\u3069\u3046\u304b\u306e\u56de\u7b54\u306b\u3064\u3044\u3066\u3001\u56de\u7b54\u8005\u306e\u751f\u5e74\u304b\u3089\u7b97\u51fa\u3057\u305f\u5e74\u4ee3\u3068\u6027\u5225\u3067\u30b0\u30eb\u30fc\u30d7\u5206\u3051\u3057\u3066\u3001\u305d\u308c\u305e\u308c\u306e\u4eba\u6570\u3092\u8868\u793a\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/sunnyday-aki.ssl-lolipop.jp\/hakataben\/votes\/result\" target=\"_blank\" rel=\"noopener\">https:\/\/sunnyday-aki.ssl-lolipop.jp\/hakataben\/votes\/result<\/a><\/p>\n<p>\u305d\u308c\u305e\u308c\u306e\u8a00\u8449\u306e\u6295\u7968\u7d50\u679c\u3092\u4e00\u6c17\u306b\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u3001case\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3092\u4f7f\u7528\u3057\u307e\u3057\u305f\u3002<br \/>\n\u4e16\u4ee3\u7d5e\u308a\u8fbc\u307f\u306ecase\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002<br \/>\n\u30ab\u30e9\u30e0yesno\u306f\u30011\u3067\u4f7f\u3063\u3066\u3044\u305f\u30020\u306f\u4f7f\u3063\u3066\u3044\u306a\u304b\u3063\u305f\u3002<br \/>\nbirthyear\u306f\u751f\u5e74\u3002\u897f\u66a6\u306e\u6570\u5024\u3067\u3059\u3002sex\u306f\u6027\u5225\u3002<\/p>\n<pre>$q1 = $query-&gt;newExpr()-&gt;case()\n    -&gt;when( $query-&gt;newExpr()-&gt;add( [\n        'yesno' =&gt; 1,\n        $query-&gt;newExpr()-&gt;between('birthyear', '40\u624d\u306e\u751f\u5e74', '21\u624d\u306e\u751f\u5e74' ),\n    ] ) )\n    -&gt;then( 1 )-&gt;else(0);\n<\/pre>\n<p>\u4e0a\u8a18\u306f\u300121\u301c40\u624d\u3067\u4f7f\u3063\u3066\u3044\u305f\u4eba\u3092\u7d5e\u308a\u8fbc\u3080case\u3002<\/p>\n<pre>$q2 = $query-&gt;newExpr()-&gt;case()\n    -&gt;when( $query-&gt;newExpr()-&gt;add( [\n        'yesno' =&gt; 0,\n        'sex' =&gt; 1,\n        $query-&gt;newExpr()-&gt;between('birthyear', '60\u624d\u306e\u751f\u5e74', '41\u624d\u306e\u751f\u5e74' ),\n    ] ) )\n    -&gt;then( 1 )-&gt;else(0);\n<\/pre>\n<p>\u4e0a\u8a18\u306f\u300141\u301c60\u624d\u306e\u7537\u6027\u3067\u4f7f\u3063\u3066\u3044\u306a\u304b\u3063\u305f\u4eba\u3092\u7d5e\u308a\u8fbc\u3080case\u3002<br \/>\nwhen\u306e\u6761\u4ef6\u306b\u30de\u30c3\u30c1\u3059\u308b\u30681\u304c\u3001\u30de\u30c3\u30c1\u3057\u306a\u3051\u308c\u30700\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002<br \/>\n\u3053\u308c\u3089\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306bsum\u3067\u96c6\u8a08\u3059\u308c\u3070\u3001\u305d\u308c\u305e\u308c\u306e\u8a00\u8449\u306e\u4e16\u4ee3\u5225\u3001\u56de\u7b54\u5225\u306e\u4eba\u6570\u3092\u7b97\u51fa\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$query-&gt;select([\n     'age20_yes' =&gt; $query-&gt;func()-&gt;sum($q1),\n     'age60_m_no' =&gt; $query-&gt;func()-&gt;sum($q2),\n ]);<\/pre>\n<p>\u5b9f\u969b\u306b\u751f\u6210\u3055\u308c\u305fSQL\u6587\u3092debug.kit\u3067\u78ba\u8a8d\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>SUM(\n  CASE WHEN (\n    yesno = 0 \n    AND sex = 1 \n    AND birthyear BETWEEN 1983 \n    AND 2002\n  ) THEN 1 ELSE 0 END\n)\n<\/pre>\n<p>\u203b\u73fe\u5728\u3001\u7d76\u8cdb\u304a\u4ed5\u4e8b\u52df\u96c6\u4e2d\u3067\u3059\u3002\u8208\u5473\u3092\u304a\u6301\u3061\u306e\u304b\u305f\u306f\u3001\u4ee5\u4e0b\u306e\u554f\u3044\u5408\u308f\u305b\u30d5\u30a9\u30fc\u30e0\u304b\u3089<br \/>\n<a href=\"https:\/\/www.fourthgate.jp\/index.php\/contact\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.fourthgate.jp\/index.php\/contact\/<\/a><br \/>\n\u304b\u3089\u3001\u662f\u975e\u662f\u975e\u3054\u9023\u7d61\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u50d5\u306f\u300c\u535a\u591a\u5f01\u3067\u3044\u3053\u3046\uff01\u300d\u3068\u3044\u3046\u30b5\u30a4\u30c8\u3092\u904b\u7528\u3057\u3066\u3044\u307e\u3059\u3002 \u3053\u306e\u30b5\u30a4\u30c8\u306f\u3001\u3082\u304625\u5e74\u304f\u3089\u3044\u524d\u306bPerl\u306ecgi\u3067\u4f5c\u3063\u305f\u306e\u304c\u6700\u521d\u3067\u3001\u305d\u306e\u5f8c\u3001PHP\u3078\u79fb\u690d\u3002 \u3055\u3089\u306b\u3001CakePHP3\u5316\u3057\u3001\u73fe\u5728\u306fCakePHP4\u3067\u904b\u7528\u3057\u3066\u3044\u307e\u3059\u3002 &hellip; <a href=\"https:\/\/blog.fourthgate.jp\/?p=1589\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1589","post","type-post","status-publish","format-standard","hentry","category-web"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=\/wp\/v2\/posts\/1589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1589"}],"version-history":[{"count":10,"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=\/wp\/v2\/posts\/1589\/revisions"}],"predecessor-version":[{"id":1608,"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=\/wp\/v2\/posts\/1589\/revisions\/1608"}],"wp:attachment":[{"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fourthgate.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}