{"id":406,"date":"2022-06-10T09:49:33","date_gmt":"2022-06-10T01:49:33","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=406"},"modified":"2022-06-10T09:49:33","modified_gmt":"2022-06-10T01:49:33","slug":"yii2%e8%ae%b0%e6%95%b0%ef%bc%8c%e8%87%aa%e5%8a%a8%e7%b4%af%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2022\/06\/10\/yii2%e8%ae%b0%e6%95%b0%ef%bc%8c%e8%87%aa%e5%8a%a8%e7%b4%af%e5%8a%a0\/","title":{"rendered":"yii2\u8bb0\u6570\uff0c\u81ea\u52a8\u7d2f\u52a0"},"content":{"rendered":"\n<p>\u6587\u7ae0\u4e00\u822c\u90fd\u6709\u7edf\u8ba1\u6d4f\u89c8\u6b21\u6570\u7684\u9700\u6c42\uff0c\u4e00\u822c\u5c0f\u578b\u9879\u76ee\u7684\u505a\u6cd5\u5c31\u662f\u76f4\u63a5 update \u6570\u636e\u5e93\u4e2d\u7684\u67d0\u4e2a\u5b57\u6bb5\u3002\u5728 Yii \u4e2d\u600e\u4e48\u5b9e\u73b0\u5462\uff1f\u8bf7\u5f80\u4e0b\u770b\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u65b9\u5f0f\u4e00<\/p>\n\n\n\n<p>Yii2 \u4e2d\u6709\u8fd9\u4e2a&nbsp;<code>updateAllCounters<\/code>&nbsp;\u9759\u6001\u65b9\u6cd5\uff0c\u8fd9\u79cd\u65b9\u5f0f\u662f\u6700\u5feb\u5e76\u4e14\u6700\u7701\u4e8b\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Topic::updateAllCounters(['view_count' => 1], ['id' => $id]);\/\/ \u5b9e\u73b0\u7684\u6548\u679c\u5c31\u662f view_count + 1\uff0c1\u6839\u636e\u4f60\u7684\u9700\u6c42\u53ef\u4ee5\u662f\u6b63\u6570\u4e5f\u53ef\u4ee5\u662f\u8d1f\u6570\u3002<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u6761\u4ef6\u6bd4\u8f83\u590d\u6742\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Topic::updateAllCounters(['view_count' => 1], ['and', ['xxx' => 0, 'yyy' => 2], ['>', 'zzz', $time]);\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u73b0\u5728\u7684\u9700\u6c42\u662f\uff1a\u66f4\u65b0\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\uff0c\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c +1\uff0c\u4f60\u53ef\u4ee5\u8bd5\u7740\u7528\u8fd9\u79cd\u65b9\u5f0f\u53bb\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Topic::updateAll(    ['view_count' => new Expression('`view_count` + 1'), 'updated_at' => time()],    ['id' => $id]);<\/code><\/pre>\n\n\n\n<p>PS\uff1a\u6b64\u5904\u7684&nbsp;<code>view_count<\/code>&nbsp;\u5b57\u6bb5\u9ed8\u8ba4\u503c\u5207\u8bb0\u4e0d\u80fd\u8bbe\u7f6e\u4e3a&nbsp;<code>null<\/code>\u3002<\/p>\n\n\n\n<p><code>Expression<\/code>&nbsp;\u662f\u8868\u8fbe\u5f0f\u7684\u610f\u601d\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u591a\u7279\u6b8a SQL\uff0c\u60f3\u4e86\u89e3\u66f4\u591a\uff0c\u53ef\u4ee5\u53bb\u641c\u7d22\u4e00\u4e0b\u6587\u6863\u3002<\/p>\n\n\n\n<p>\u65b9\u5f0f\u4e8c<\/p>\n\n\n\n<p>\u5f53\u7136\u4f60\u8fd8\u53ef\u4ee5\u8fd9\u6837\u53bb\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$model = Post::findOne($id);$model->updateCounters(['view_count' => 1]);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u4e00\u822c\u90fd\u6709\u7edf\u8ba1\u6d4f\u89c8\u6b21\u6570\u7684\u9700\u6c42\uff0c\u4e00\u822c\u5c0f\u578b\u9879\u76ee\u7684\u505a\u6cd5\u5c31\u662f\u76f4\u63a5 update \u6570\u636e\u5e93\u4e2d\u7684\u67d0\u4e2a\u5b57\u6bb5\u3002\u5728 Yii \u4e2d\u600e\u4e48\u5b9e\u73b0\u5462\uff1f\u8bf7\u5f80\u4e0b\u770b\uff1a \u5b9e\u73b0 \u65b9\u5f0f\u4e00 Yii2 \u4e2d\u6709\u8fd9\u4e2a&nbsp;updateAllCounters&nbsp;\u9759\u6001\u65b9\u6cd5\uff0c\u8fd9\u79cd\u65b9\u5f0f\u662f\u6700\u5feb\u5e76\u4e14\u6700\u7701\u4e8b\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a \u5982\u679c\u4f60\u7684\u6761\u4ef6\u6bd4\u8f83\u590d\u6742\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a \u5982\u679c\u4f60\u73b0\u5728\u7684\u9700\u6c42\u662f\uff1a\u66f4\u65b0\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\uff0c\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c +1\uff0c\u4f60\u53ef\u4ee5\u8bd5\u7740\u7528\u8fd9\u79cd\u65b9\u5f0f\u53bb\u5b9e\u73b0\uff1a PS\uff1a\u6b64\u5904\u7684&nbsp;view_count&nbsp;\u5b57\u6bb5\u9ed8\u8ba4\u503c\u5207\u8bb0\u4e0d\u80fd\u8bbe\u7f6e\u4e3a&nbsp;null\u3002 Expression&nbsp;\u662f\u8868\u8fbe\u5f0f\u7684\u610f\u601d\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u591a\u7279\u6b8a SQL\uff0c\u60f3\u4e86\u89e3\u66f4\u591a\uff0c\u53ef\u4ee5\u53bb\u641c\u7d22\u4e00\u4e0b\u6587\u6863\u3002 \u65b9\u5f0f\u4e8c \u5f53\u7136\u4f60\u8fd8\u53ef\u4ee5\u8fd9\u6837\u53bb\u5b9e\u73b0\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-406","post","type-post","status-publish","format-standard","hentry","category-yii2"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}