{"id":411,"date":"2019-07-09T16:37:54","date_gmt":"2019-07-09T07:37:54","guid":{"rendered":"https:\/\/took.jp\/?p=411"},"modified":"2020-06-15T16:49:26","modified_gmt":"2020-06-15T07:49:26","slug":"411","status":"publish","type":"post","link":"https:\/\/took.jp\/tech-blog\/411\/","title":{"rendered":"cakePHP2.x\u7cfb\u3000\u57fa\u672c\u7684\u306aSQL\u30af\u30a8\u30ea\u306e\u767a\u884c\u65b9\u6cd5"},"content":{"rendered":"<p><span>cakephp\u306e\u30d8\u30eb\u30d1\u30fc\u3067\u4f5c\u6210\u3059\u308bsql\u6587\u3063\u3066\u6700\u521d\u306f\u51c4\u304f\u308f\u304b\u308a\u306b\u304f\u3044\u3093\u3067\u3059\u3088\u306d\u3002<\/span><\/p>\n<p>\u3067\u3082\u6163\u308c\u308b\u3068\u3001\u3053\u308c\u306f\u3053\u308c\u3067\u826f\u3044\u3068\u601d\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001<strong>cakePHP2\u7cfb<\/strong>\u306e\u60c5\u5831\u304c\u5c11\u306a\u3044\u306e\u3067\u3001\u57fa\u672c\u7684\u306aSQL\u30af\u30a8\u30ea\u306e\u767a\u884c\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>insert\u3001update\u6587<\/h2>\n<p>\u4e21\u65b9\u3068\u3082update\u6587\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-php\">public function setUserData($param) {\r\n\u3000$data = array(\r\n\u3000\u3000\"id\"       =&gt; null,\r\n\u3000\u3000\"name\"     =&gt; $param['name'],\r\n\u3000\u3000\"password\" =&gt; md5($param['password']),\r\n\u3000);\r\n\u3000return $this-&gt;save($data);\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u8a66\u3057\u306b\u66f8\u3044\u3066\u307f\u308b\u3068\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<p>\u30c6\u30fc\u30d6\u30eb\u540d\u306f\u3001\u57fa\u672c\u306f\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u8a18\u8ff0\u3057\u3066\u3044\u308b\u30e2\u30c7\u30eb\u3068\u540c\u3058\u3082\u306e\u306b\u306a\u308b\u306e\u3067\u66f8\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>data\u914d\u5217\u306e<strong>\u30ad\u30fc<\/strong>\u306f\u3001<strong>DB\u306e\u30ab\u30e9\u30e0\u540d<\/strong><\/p>\n<p><strong>\u5024<\/strong>\u306f\u3001<strong>\u4fdd\u5b58\u3057\u305f\u3044\u5024<\/strong>\u3092\u5165\u308c\u3066\u3084\u308a\u307e\u3059\u3002<\/p>\n<p>id\u30ab\u30e9\u30e0\u306f\u3001\u4e3b\u30ad\u30fc\u5236\u7d04\u3068auto_increment\u304c\u8a2d\u5b9a\u3057\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u3082\u3057\u3001id\u306e\u5024\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u305f\u5834\u5408\u3001<\/strong><\/p>\n<p>\u4e3b\u30ad\u30fc\u5236\u7d04\u3067\u91cd\u8907\u304c\u8a8d\u3081\u3089\u308c\u306a\u3044\u306e\u3067\u3001update\u6587\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><strong>\u3082\u3057\u3001id\u306e\u5024\u304cnull\u3060\u3063\u305f\u5834\u5408<\/strong><\/p>\n<p>auto_increment\u3067\u91cd\u8907\u3057\u306a\u3044\u4e00\u756a\u65b0\u3057\u3044\u6570\u5b57\u3067id\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001\u4e0a\u8a18\u306e\u4f8b\u6587\u306e\u5834\u5408\u306f\u3001insert\u6587\u3092\u5b9f\u884c\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f8b\u6587\u3092sql\u306b\u66f8\u304d\u306a\u304a\u3059\u3068\u3053\u3046\u3067\u3059\u306d<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">insert into \u30e2\u30c7\u30eb\u540d (id, name, password) values(null, $param['name'], md5($param['password']));<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>select\u6587<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-php\">public function getUserName($user_id){\r\n\u3000$options = array(\r\n\u3000\u3000'conditions' =&gt; array(\r\n\u3000\u3000\u3000'user_id' =&gt; $user_id,\r\n\u3000\u3000),\r\n\u3000\u3000'fields' =&gt; array(\r\n\u3000\u3000\u3000'id',\r\n\u3000\u3000\u3000'name',\r\n\u3000\u3000),\r\n\u3000);\r\n\u3000return $this-&gt;find('all', $options);\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u308c\u306f\u3001user_id\u3092\u57fa\u306b\u305d\u306e\u30e6\u30fc\u30b6\u306eid\u3068\u540d\u524d\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<p>option\u914d\u5217\u306e\u4e2d\u306b\u3042\u308b<\/p>\n<p><strong>field<\/strong>\u3068\u3044\u3046\u306e\u306f\u3001<strong>SELECT\u53e5<\/strong><\/p>\n<p><strong>conditions<\/strong>\u3068\u3044\u3046\u306e\u306f\u3001<strong>WHERE\u53e5<\/strong><\/p>\n<p>\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3053\u308c\u3092sql\u306b\u66f8\u304d\u306a\u304a\u3059\u3068\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-sql\">select id, name from \u30e2\u30c7\u30eb\u540d where user_id = $user_id;<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>cakephp\u306e\u30d8\u30eb\u30d1\u30fc\u3067\u4f5c\u6210\u3059\u308bsql\u6587\u3063\u3066\u6700\u521d\u306f\u51c4\u304f\u308f\u304b\u308a\u306b\u304f\u3044\u3093\u3067\u3059\u3088\u306d\u3002 \u3067\u3082\u6163\u308c\u308b\u3068\u3001\u3053\u308c\u306f\u3053\u308c\u3067\u826f\u3044\u3068\u601d\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002 \u305f\u3060\u3001cakePHP2\u7cfb\u306e\u60c5\u5831\u304c\u5c11\u306a\u3044\u306e\u3067\u3001\u57fa\u672c\u7684\u306aSQL\u30af\u30a8\u30ea\u306e\u767a\u884c\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002 &#038;nb\u2026<\/p>\n","protected":false},"author":3,"featured_media":1983,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84,62,67],"tags":[],"class_list":["post-411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cakephp","category-it","category-php"],"_links":{"self":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/comments?post=411"}],"version-history":[{"count":0,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/411\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media\/1983"}],"wp:attachment":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media?parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/categories?post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/tags?post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}