{"id":47695,"date":"2024-12-02T10:07:54","date_gmt":"2024-12-02T02:07:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47695\/"},"modified":"2024-12-02T10:07:54","modified_gmt":"2024-12-02T02:07:54","slug":"php-%e5%8f%82%e6%95%b0%e7%bb%91%e5%ae%9a%e4%b8%8e-orm-%e6%a1%86%e6%9e%b6%e7%9a%84%e9%9b%86%e6%88%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47695\/","title":{"rendered":"PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>\u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\u7684\u5b89\u5168\u6570\u636e\u5e93\u64cd\u4f5c\u6280\u672f\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230 SQL \u8bed\u53e5\uff0c\u5b83\u6d89\u53ca\uff1a\u51c6\u5907 SQL \u8bed\u53e5\u5e76\u521b\u5efa\u53d8\u91cf\u6570\u7ec4\u3002\u4f7f\u7528 PDO \u6216 ORM \u6846\u67b6\u8fdb\u884c\u7ed1\u5b9a\u548c\u6267\u884c\u3002\u96c6\u6210\u5230 ORM \u6846\u67b6\u540e\uff0c\u5b83\u7b80\u5316\u4e86\u53c2\u6570\u7ed1\u5b9a\uff0c\u81ea\u52a8\u751f\u6210\u5b89\u5168\u7684 SQL \u67e5\u8be2\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729930822671ca646294a2.jpg\" class=\"aligncenter\" title=\"PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210\u63d2\u56fe\" alt=\"PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u96c6\u6210<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u5b89\u5168\u5feb\u6377\u5730\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6280\u672f\uff0c\u5b83\u901a\u8fc7\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230 SQL \u8bed\u53e5\u7684\u7279\u5b9a\u4f4d\u7f6e\uff0c\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5728 PHP \u4e2d\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\uff0c\u91cd\u70b9\u5173\u6ce8\u5176\u4e0e ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff09\u6846\u67b6\u7684\u96c6\u6210\u3002<\/p>\n<p><strong>\u53c2\u6570\u7ed1\u5b9a\u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/p>\n<p>\u53c2\u6570\u7ed1\u5b9a\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u51c6\u5907\u4e00\u4e2a\u5e26\u95ee\u53f7 (?) \u7684 SQL \u8bed\u53e5\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u8981\u7ed1\u5b9a\u53d8\u91cf\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528 PDO <code>prepare()<\/code> \u65b9\u6cd5\u51c6\u5907\u8bed\u53e5\u3002<\/li>\n<li>\u6839\u636e\u7ed1\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u4f7f\u7528 <code>bindParam()<\/code> \u6216 <code>bindValue()<\/code> \u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230\u5bf9\u5e94\u4f4d\u7f6e\u3002<\/li>\n<li>\u6267\u884c\u8bed\u53e5\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p><strong>\u4f7f\u7528 PDO \u8fdb\u884c\u53c2\u6570\u7ed1\u5b9a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 PHP Data Objects (PDO) \u8fdb\u884c\u53c2\u6570\u7ed1\u5b9a\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>$sql = \"SELECT * FROM users WHERE username = ?\";\n\n$stmt = $pdo-&gt;prepare($sql);\n$stmt-&gt;bindParam(1, $username, PDO::PARAM_STR);\n$stmt-&gt;execute();<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>$username<\/code> \u53d8\u91cf\u88ab\u7ed1\u5b9a\u5230\u4f4d\u7f6e 1\uff0c\u5b83\u5c06\u88ab\u66ff\u6362\u4e3a\u5e26\u6709\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<p><strong>\u5728 ORM \u6846\u67b6\u4e2d\u96c6\u6210\u53c2\u6570\u7ed1\u5b9a<\/strong><\/p>\n<p>ORM \u6846\u67b6\u7b80\u5316\u4e86\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\uff0c\u5e76\u7b80\u5316\u4e86\u53c2\u6570\u7ed1\u5b9a\u7684\u8fc7\u7a0b\u3002\u6bcf\u4e2a ORM \u6846\u67b6\u5bf9\u53c2\u6570\u7ed1\u5b9a\u7684\u5904\u7406\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\uff0c\u4f46\u4e00\u822c\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f53\u5bf9\u8c61\u3002<\/li>\n<li>\u4e3a\u5b9e\u4f53\u5c5e\u6027\u8bbe\u7f6e\u503c\u3002<\/li>\n<li>\u4f7f\u7528 ORM \u6846\u67b6\u7684\u4fdd\u5b58\u6216\u66f4\u65b0\u65b9\u6cd5\u5c06\u5b9e\u4f53\u4fdd\u5b58\u6216\u66f4\u65b0\u5230\u6570\u636e\u5e93\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>User<\/code> \u7684\u6a21\u578b\uff0c\u5b83\u5305\u542b <code>username<\/code> \u548c <code>email<\/code> \u5c5e\u6027\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Laravel \u7684 Eloquent ORM \u6846\u67b6\u5728\u63a7\u5236\u5668\u4e2d\u5bf9\u5176\u8fdb\u884c\u66f4\u65b0\uff1a<\/p>\n<pre>public function update(User $user)\n{\n    $user-&gt;username = \"new_username\";\n    $user-&gt;email = \"new_email@example.com\";\n\n    $user-&gt;save();\n}<\/pre>\n<p>\u5728\u5e55\u540e\uff0cEloquent \u4f1a\u81ea\u52a8\u5904\u7406\u53c2\u6570\u7ed1\u5b9a\uff0c\u751f\u6210\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684 SQL \u67e5\u8be2\uff1a<\/p>\n<pre>UPDATE users SET username = \"new_username\", email = \"new_email@example.com\" WHERE id = {id}<\/pre>\n<p><strong>Conclusion<\/strong><\/p>\n<p>\u53c2\u6570\u7ed1\u5b9a\u662f\u7f16\u5199\u5b89\u5168\u7684 PHP \u6570\u636e\u5e93\u4ee3\u7801\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u901a\u8fc7\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5316\u53c2\u6570\u7ed1\u5b9a\u7684\u8fc7\u7a0b\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210 \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u7684\u96c6\u6210\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 \u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\u7684\u5b89\u5168\u6570\u636e\u5e93\u64cd\u4f5c\u6280\u672f\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230 SQL \u8bed\u53e5\uff0c\u5b83\u6d89\u53ca\uff1a\u51c6\u5907 SQL \u8bed\u53e5\u5e76\u521b\u5efa\u53d8\u91cf\u6570\u7ec4\u3002\u4f7f\u7528 PDO \u6216 ORM \u6846\u67b6\u8fdb\u884c\u7ed1\u5b9a\u548c\u6267\u884c\u3002\u96c6\u6210\u5230 ORM \u6846\u67b6\u540e\uff0c\u5b83\u7b80\u5316\u4e86\u53c2\u6570\u7ed1\u5b9a\uff0c\u81ea\u52a8\u751f\u6210\u5b89\u5168\u7684 SQL \u67e5\u8be2\u3002 PHP \u53c2\u6570\u7ed1\u5b9a\u4e0e ORM \u6846\u67b6\u96c6\u6210 \u5f15\u8a00 \u53c2\u6570\u7ed1\u5b9a\u662f\u4e00\u79cd\u5b89\u5168\u5feb\u6377\u5730\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6280\u672f\uff0c\u5b83\u901a\u8fc7\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230 SQL \u8bed\u53e5\u7684\u7279\u5b9a\u4f4d\u7f6e\uff0c\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5728 PHP \u4e2d\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\uff0c\u91cd\u70b9\u5173\u6ce8\u5176\u4e0e ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff09\u6846\u67b6\u7684\u96c6\u6210\u3002 \u53c2\u6570\u7ed1\u5b9a\u7684\u5de5\u4f5c\u539f\u7406 \u53c2\u6570\u7ed1\u5b9a\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u51c6\u5907\u4e00\u4e2a\u5e26\u95ee\u53f7 (?) \u7684 SQL \u8bed\u53e5\u3002 \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u8981\u7ed1\u5b9a\u53d8\u91cf\u7684\u6570\u7ec4\u3002 \u4f7f\u7528 PDO prepare() \u65b9\u6cd5\u51c6\u5907\u8bed\u53e5\u3002 \u6839\u636e\u7ed1\u5b9a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u4f7f\u7528 bindParam() \u6216 bindValue() \u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230\u5bf9\u5e94\u4f4d\u7f6e\u3002 \u6267\u884c\u8bed\u53e5\u5e76\u83b7\u53d6\u7ed3\u679c\u3002 \u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47695","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47695"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47695\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}