{"id":905,"date":"2016-08-15T16:11:14","date_gmt":"2016-08-15T08:11:14","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=905"},"modified":"2016-08-16T17:25:57","modified_gmt":"2016-08-16T09:25:57","slug":"yii%e6%a1%86%e6%9e%b6%e4%b8%ad%e7%9a%84curd%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=905","title":{"rendered":"Yii createCommand CURD\u64cd\u4f5c"},"content":{"rendered":"<p>\u672c\u6587\u7528\u4f5c\u5de5\u4f5c\u8bb0\u5f55\uff0c\u4e5f\u8bb8\u6709\u4eba\u4f1a\u95ee\u4e3a\u4ec0\u4e48\u4e0d\u7528 Yii \u7684 Model \u53bb\u64cd\u4f5c DB\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff0cYii \u7684 Model \u5199\u6cd5\u4e0a\u662f\u65b9\u4fbf\u4e86\u5f88\u591a\uff0c\u4f46\u662f\u4f1a\u6267\u884c\u591a\u4f59\u7684 SQL\uff0c\u6253\u5f00 Yii \u7684\u6267\u884c log \u5c31\u4f1a\u53d1\u73b0\u3002\u6240\u4ee5\u4e3a\u4e86\u6548\u7387\uff0c\u4e3a\u4e86 DB \u670d\u52a1\u5668\u7684\u6027\u80fd\u8003\u8651\uff0c\u8fd8\u662f\u4f7f\u7528\u00a0createCommand \u7684\u597d\u3002<\/p>\n<p>insert<\/p>\n<pre class=\"lang:default decode:true \">$row = Yii::app()-&gt;getDb()-&gt;createCommand()-&gt;insert('goods', array(\r\n            'good_name' =&gt; $goods_name,\r\n            'good_type' =&gt; $goods_type,\r\n            'price' =&gt; $price,\r\n            'buy_nums' =&gt; 0,\r\n            'commit_nums' =&gt; 0,\r\n            'create_time' =&gt; time(),\r\n        ));<\/pre>\n<p>select\u00a0\u00a0\u5355\u8868\u67e5\u8be2<\/p>\n<pre class=\"lang:default decode:true \">$goodsTypes = Yii::app()-&gt;getDb()-&gt;createCommand()\r\n            -&gt;select('type_id, type_name')\r\n            -&gt;from('goods_type')\r\n            -&gt;where('status=1')-&gt;queryAll();<\/pre>\n<p>\u8fde\u8868\u67e5\u8be2<\/p>\n<pre class=\"lang:default decode:true \">$goods = Yii::app()-&gt;getDb()-&gt;createCommand()-&gt;from('goods g')\r\n        -&gt;select('g.good_id, g.good_name, gt.type_name, g.price, g.buy_nums, g.commit_nums, g.create_time')\r\n        -&gt;join('goods_type gt', 'g.good_type=gt.type_id')\r\n        -&gt;where('g.`status`=1 and gt.`status`=1')\r\n        -&gt;order('g.create_time desc')\r\n        -&gt;queryAll();<\/pre>\n<p>delete<\/p>\n<pre class=\"lang:default decode:true \">$row = Yii::app()-&gt;getDb()-&gt;createCommand()\r\n        -&gt;delete('goods', \"good_id=:good_id\", array(\r\n            ':good_id' =&gt; $goods_id,\r\n        ));<\/pre>\n<p>update<\/p>\n<pre class=\"lang:default decode:true \">$row = Yii::app()-&gt;getDb()-&gt;createCommand()-&gt;update('goods', array(\r\n    'good_name' =&gt; $goods_name,\r\n    'good_type' =&gt; $goods_type,\r\n    'price' =&gt; $price,\r\n), \"good_id='{$goods_id}'\");<\/pre>\n<p>\u5efa\u7acb\u591a\u4e2a\u67e5\u8be2<\/p>\n<p>\u4e00\u4e2a CDbCommand \u5b9e\u4f8b\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u591a\u6b21\u5efa\u7acb\u51e0\u4e2a\u67e5\u8be2\u3002\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u67e5\u8be2\u4e4b\u524d\uff0c\u9700\u8981\u8c03\u7528 CDbCommand::reset() \u65b9\u6cd5\u4ee5\u6e05\u7406\u524d\u9762\u7684\u67e5\u8be2\u3002\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$command = Yii::app()-&gt;db-&gt;createCommand();\r\n$users = $command-&gt;select('*')-&gt;from('tbl_users')-&gt;queryAll();\r\n$command-&gt;reset(); \/\/ clean up the previous query\r\n$posts = $command-&gt;select('*')-&gt;from('tbl_posts')-&gt;queryAll();\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Yii::app()-&gt;db-&gt;createCommand($sql)-&gt;queryAll(); \/\/\u67e5\u8be2\u6240\u6709\u884c\u6570\u636e<br \/>\u200bYii::app()-&gt;db-&gt;createCommand($sql)-&gt;queryRow(); \/\/\u67e5\u8be2\u7b2c\u4e00\u884c\u6570\u636e<br \/>\u200bYii::app()-&gt;db-&gt;createCommand($sql)-&gt;queryColumn(); \/\/\u67e5\u8be2\u7b2c\u4e00\u5217\u6570\u636e\u200b<br \/>\u200bYii::app()-&gt;db-&gt;createCommand($sql)-&gt;queryScalar(); \/\/\u67e5\u8be2\u7b2c\u4e00\u884c\u7684\u7b2c\u4e00\u5b57\u6bb5<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ \u9996\u5148\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2aCDbCommand\u5bf9\u8c61 <br \/>$command = Yii::app()-&gt;db-&gt;createCommand(); \/\/ \u6ce8\u610f\u53c2\u6570\u7559\u7a7a\u4e86\u3002\u3002 <br \/>\/\/ \u53ef\u7528\u7684\u65b9\u6cd5\u5217\u8868\u5982\u4e0b\uff1a <br \/>-&gt;select(): SELECT\u5b50\u53e5 <br \/>-&gt;selectDistinct(): SELECT\u5b50\u53e5\uff0c\u5e76\u4fdd\u6301\u4e86\u8bb0\u5f55\u7684\u552f\u4e00\u6027 <br \/>-&gt;from(): \u6784\u5efaFROM\u5b50\u53e5 <br \/>-&gt;where(): \u6784\u5efaWHERE\u5b50\u53e5 <br \/>-&gt;join(): \u5728FROM\u5b50\u53e5\u4e2d\u6784\u5efaINNER JOIN \u5b50\u53e5 <br \/>-&gt;leftJoin(): \u5728FROM\u5b50\u53e5\u4e2d\u6784\u5efa\u5de6\u8fde\u63a5\u5b50\u53e5 <br \/>-&gt;rightJoin(): \u5728FROM\u5b50\u53e5\u4e2d\u6784\u5efa\u53f3\u8fde\u63a5\u5b50\u53e5 <br \/>-&gt;crossJoin(): \u6dfb\u52a0\u4ea4\u53c9\u67e5\u8be2\u7247\u6bb5(\u6ca1\u7528\u8fc7) <br \/>-&gt;naturalJoin(): \u6dfb\u52a0\u4e00\u4e2a\u81ea\u7136\u8fde\u63a5\u5b50\u7247\u6bb5 <br \/>-&gt;group(): GROUP BY\u5b50\u53e5 <br \/>-&gt;having(): \u7c7b\u4f3c\u4e8eWHERE\u7684\u5b50\u53e5\uff0c\u4f46\u8981\u4e0eGROUP BY\u8fde\u7528 <br \/>-&gt;order(): ORDER BY\u5b50\u53e5 <br \/>-&gt;limit(): LIMIT\u5b50\u53e5\u7684\u7b2c\u4e00\u90e8\u5206 <br \/>-&gt;offset(): LIMIT\u5b50\u53e5\u7684\u7b2c\u4e8c\u90e8\u5206 <br \/>-&gt;union(): appends a UNION query fragment<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7528\u4f5c\u5de5\u4f5c\u8bb0\u5f55\uff0c\u4e5f\u8bb8\u6709\u4eba\u4f1a\u95ee\u4e3a\u4ec0\u4e48\u4e0d\u7528 Yii \u7684 Model \u53bb\u64cd\u4f5c DB\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff0cYii \u7684 Mod [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[48],"class_list":["post-905","post","type-post","status-publish","format-standard","hentry","category-php","tag-yii"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=905"}],"version-history":[{"count":5,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/905\/revisions"}],"predecessor-version":[{"id":908,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/905\/revisions\/908"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}