{"id":200245,"date":"2025-05-06T11:25:41","date_gmt":"2025-05-06T03:25:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200245\/"},"modified":"2025-05-06T11:25:41","modified_gmt":"2025-05-06T03:25:41","slug":"%e5%9c%a8%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e6%9b%b4%e6%96%b0%e6%8f%92%e5%85%a5%e4%b8%8d%e4%bd%bf%e7%94%a8%e4%b8%bb%e9%94%ae%e6%88%96%e5%94%af%e4%b8%80%e7%ba%a6%e6%9d%9f%e7%9a%84%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200245\/","title":{"rendered":"\u5728\u6570\u636e\u5e93\u4e2d\u66f4\u65b0\u63d2\u5165\u4e0d\u4f7f\u7528\u4e3b\u952e\u6216\u552f\u4e00\u7ea6\u675f\u7684\u884c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728\u6570\u636e\u5e93\u4e2d\u66f4\u65b0\u63d2\u5165\u4e0d\u4f7f\u7528\u4e3b\u952e\u6216\u552f\u4e00\u7ea6\u675f\u7684\u884c<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u4e3b\u673a\u5b9d\u8d1d\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5728\u6570\u636e\u5e93\u4e2d\u66f4\u65b0\u63d2\u5165\u4e0d\u4f7f\u7528\u4e3b\u952e\u6216\u552f\u4e00\u7ea6\u675f\u7684\u884c\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/173025544967219a597f853.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728\u6211 7 \u5e74\u7684\u7a0b\u5e8f\u5458\u804c\u4e1a\u751f\u6daf\u4e2d\uff0c\u6211\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u662f\u901a\u8fc7 orm \u4e0e sql \u4ea4\u4e92\u3002\u6211\u53d1\u73b0 laravel \u7684 eloquent orm \u7684\u4e00\u4e2a\u7279\u522b\u6709\u7528\u7684\u529f\u80fd\u662f\u5b83\u7684 updateorinsert() \u65b9\u6cd5\uff1a<\/p>\n<pre>db::table('posts')\n    -&gt;updateorinsert(\n        ['slug' =&gt; 'about'], \/\/ matching condition\n        ['content' =&gt; 'like and subscribe'] \/\/ created or updated values\n    );\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0celoquent \u5c06\u5728 posts \u8868\u4e2d\u67e5\u627e slug \u7b49\u4e8e\u201cabout\u201d\u7684\u884c\u3002\u5982\u679c\u5b58\u5728\u5e26\u6709\u8be5 slug \u7684\u884c\uff0celoquent \u4f1a\u5c06\u8be5\u884c\u7684\u5185\u5bb9\u66f4\u65b0\u4e3a\u201c\u70b9\u8d5e\u5e76\u8ba2\u9605\u201d\u3002\u5982\u679c\u884c<em>\u4e0d<\/em>\u5b58\u5728\u4e8e\u8be5slug\u4e2d\uff0celoquent\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u884c\uff0c\u5176\u4e2d\u5305\u542b\u201cabout\u201d\u548c\u201clike and subscribe\u201d\u7684\u5185\u5bb9\u3002<\/p>\n<h2> \u95ee\u9898\uff1a\u6ca1\u6709\u4e3b\u952e\u6216\u552f\u4e00\u7ea6\u675f <\/h2>\n<p>\u6211\u76ee\u524d\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u8fc1\u79fb\u811a\u672c\uff0c\u7528\u4e8e\u5c06\u9875\u9762\u6570\u636e\u4ece\u65e7\u7684 wordpress \u7ad9\u70b9\u79fb\u52a8\u5230\u65b0\u7684 wordpress \u7ad9\u70b9\u3002\u8be5\u811a\u672c\u8fde\u63a5\u5230\u65e7\u7ad9\u70b9\u7684\u6570\u636e\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u5bfc\u5165\u5230\u65b0\u7ad9\u70b9\u7684\u6570\u636e\u5e93\u4e2d\u7684 sql \u6587\u4ef6\u3002\u65b0\u7f51\u7ad9\u5df2\u6709\u4e00\u4e9b\u5df2\u624b\u52a8\u79fb\u52a8\u7684\u9875\u9762\uff0c\u4f46\u5176\u5185\u5bb9\u53ef\u80fd\u5df2\u8fc7\u65f6\u3002\u5f53\u65b0\u7ad9\u70b9\u4e0a\u5df2\u7ecf\u5b58\u5728\u9875\u9762\u65f6\uff0c\u6211\u4eec\u4e0d\u60f3\u518d\u6b21\u521b\u5efa\u5b83\uff1a\u6211\u4eec\u60f3\u8981\u66f4\u65b0\u5df2\u7ecf\u5b58\u5728\u7684\u9875\u9762\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 wordpress \u6570\u636e\u5e93\u4e2d\u7684 post_name \u5217\u6765\u786e\u5b9a\u8be5\u9875\u9762\u662f\u5426\u5df2\u7ecf\u5b58\u5728\uff0c\u8be5\u5217\u5bf9\u5e94\u4e8e\u9875\u9762\u7684 url slug\u3002<\/p>\n<p>\u5982\u679c post_name \u662f\u4e3b\u952e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 replace \u8bed\u53e5\u5b8c\u6210\u7c7b\u4f3c\u4e8e eloquent \u7684 createorupdate() \u65b9\u6cd5\u7684\u64cd\u4f5c\uff0c\u4f46 post_name \u4e0d\u662f\u4e3b\u952e\u3002<\/p>\n<p>\u5982\u679c post_name \u6709\u552f\u4e00\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 insert &#8230; on duplicate key update \u8bed\u53e5\u5b8c\u6210\u7c7b\u4f3c\u4e8e eloquent \u7684 createorupdate() \u65b9\u6cd5\u7684\u64cd\u4f5c\uff0c\u4f46 post_name \u6ca1\u6709\u552f\u4e00\u7ea6\u675f\u3002<\/p>\n<p>\u554a\uff0cwordpress \u7684\u4e50\u8da3\u3002<\/p>\n<p>\u6211\u7814\u7a76\u4e86 mysql \u7684 if \u8bed\u53e5\uff0c\u4f46 if \u8bed\u53e5\u4ec5\u9002\u7528\u4e8e\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u548c\u51fd\u6570\u3002\u6211\u4e0d\u60f3\u5728\u8981\u5bfc\u5165\u5230\u65b0\u7ad9\u70b9\u6570\u636e\u5e93\u7684 sql \u6587\u4ef6\u4e2d\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\uff0c\u56e0\u6b64\u6211\u5fc5\u987b\u641c\u7d22\u5176\u4ed6\u9009\u9879\u3002<\/p>\n<h2> \u89e3\u51b3\u65b9\u6848\uff1a2\u6761\u8bed\u53e5 <\/h2>\n<p>\u6211\u80fd\u627e\u5230\u7684\u5728\u7eaf sql \u4e2d\u6a21\u62df eloquent \u7684 updateorinsert() \u529f\u80fd\u7684\u6700\u7b80\u5355\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u95ee\u9898\u5206\u6210\u4e24\u90e8\u5206\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528\u5339\u914d\u7684 slug \u66f4\u65b0\u4efb\u4f55\u73b0\u6709\u884c\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u5339\u914d slugs \u7684\u884c\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u884c\u3002<\/li>\n<\/ol>\n<p>\u5b9e\u9645\u60c5\u51b5\u5982\u4e0b\uff1a<\/p>\n<pre>-- Update the post if it already exists.\n\nUPDATE wp_posts\nSET post_type = 'page',\n    post_title = 'About',\n    post_content = 'Like and subscribe'\nWHERE post_name = 'about';\n\n-- Create a new post if it does not exist.\n\nINSERT INTO wp_posts (post_name, post_type, post_title, post_content)\nSELECT 'about', 'page', 'About', 'Like and subscribe'\nWHERE NOT EXISTS (\n    SELECT 1\n    FROM wp_posts\n    WHERE post_name = 'about'\n);\n<\/pre>\n<blockquote>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u4e3a\u4e86\u7b80\u6d01\u548c\u6e05\u6670\u8d77\u89c1\uff0c\u6b64\u793a\u4f8b\u7701\u7565\u4e86\u8bb8\u591a wordpress \u6240\u9700\u7684 wp_posts \u5217\u3002<\/p>\n<\/blockquote>\n<p>\u4e86\u89e3 insert &#8230; select \u8bed\u53e5\u5bf9\u6211\u6765\u8bf4\u662f\u201c\u987f\u609f\u201d\u65f6\u523b\u3002\u5b83\u7684\u76ee\u7684\u662f\u4f7f\u7528\u53e6\u4e00\u4e2a\u8868\u7684\u67e5\u8be2\u7ed3\u679c\u6765\u4e00\u6b21\u6267\u884c\u591a\u6b21\u63d2\u5165\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u81ea\u5df1\u7684\u503c\u6765\u4f7f\u7528\u548c\u6ee5\u7528 sql \u7684\u529f\u80fd\uff0c\u5e76\u4e14\u4ec5\u5728 post_name \u4e3a\u201cabout\u201d\u7684\u5e16\u5b50\u4e0d\u5b58\u5728\u65f6\u624d\u6267\u884c\u63d2\u5165\u3002<\/p>\n<p>\u8fd9\u662f\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6700\u4f18\u96c5\u3001\u6700\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u5417\uff1f\u53ef\u80fd\u4e0d\u4f1a\u3002\u4f46\u5bf9\u4e8e wordpress \u7ad9\u70b9\u7684\u4e00\u6b21\u6027\u8fc1\u79fb\u6765\u8bf4\uff0c\u5b83\u5df2\u7ecf\u8db3\u591f\u597d\u4e86\uff0c\u5e76\u4e14\u5b83\u5141\u8bb8\u60a8\u66f4\u65b0\u6216\u63d2\u5165\u4e00\u884c\uff0c\u800c\u65e0\u9700\u5b58\u50a8\u8fc7\u7a0b\u6216\u4efb\u4f55\u5e94\u7528\u7a0b\u5e8f\u903b\u8f91\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u60a8\u5728\u4e0d\u501f\u52a9 orm \u7684\u60c5\u51b5\u4e0b\u7f16\u5199\u5f3a\u5927\u7684\u67e5\u8be2\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u4e3b\u673a\u5b9d\u8d1d\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u3002<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664 <\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6570\u636e\u5e93\u4e2d\u66f4\u65b0\u63d2\u5165\u4e0d\u4f7f\u7528\u4e3b\u952e\u6216\u552f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200245","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200245"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200245\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}