tablehelper.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <!DOCTYPE HTML>
  2. <html lang="" >
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  6. <title>TableHelper · GitBook</title>
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  8. <meta name="description" content="">
  9. <meta name="generator" content="GitBook 3.2.3">
  10. <link rel="stylesheet" href="../gitbook/style.css">
  11. <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
  12. <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
  13. <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
  14. <meta name="HandheldFriendly" content="true"/>
  15. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  16. <meta name="apple-mobile-web-app-capable" content="yes">
  17. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  18. <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
  19. <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
  20. <link rel="next" href="ke-zhi-xing-jiao-ben.html" />
  21. <link rel="prev" href="param.html" />
  22. </head>
  23. <body>
  24. <div class="book">
  25. <div class="book-summary">
  26. <div id="book-search-input" role="search">
  27. <input type="text" placeholder="Type to search" />
  28. </div>
  29. <nav role="navigation">
  30. <ul class="summary">
  31. <li class="chapter " data-level="1.1" data-path="../">
  32. <a href="../">
  33. 简介
  34. </a>
  35. </li>
  36. <li class="chapter " data-level="1.2" data-path="../chapter1.html">
  37. <a href="../chapter1.html">
  38. 基础用法
  39. </a>
  40. <ul class="articles">
  41. <li class="chapter " data-level="1.2.1" data-path="../chapter1/pei-zhi.html">
  42. <a href="../chapter1/pei-zhi.html">
  43. 配置
  44. </a>
  45. </li>
  46. <li class="chapter " data-level="1.2.2" data-path="../chapter1/lu-you.html">
  47. <a href="../chapter1/lu-you.html">
  48. 路由
  49. </a>
  50. </li>
  51. <li class="chapter " data-level="1.2.3" data-path="../chapter1/kong-zhi-qi.html">
  52. <a href="../chapter1/kong-zhi-qi.html">
  53. 控制器
  54. </a>
  55. </li>
  56. <li class="chapter " data-level="1.2.4" data-path="../chapter1/mo-xing.html">
  57. <a href="../chapter1/mo-xing.html">
  58. 模型
  59. </a>
  60. </li>
  61. <li class="chapter " data-level="1.2.5" data-path="../chapter1/shi-tu.html">
  62. <a href="../chapter1/shi-tu.html">
  63. 视图
  64. </a>
  65. </li>
  66. </ul>
  67. </li>
  68. <li class="chapter " data-level="1.3" data-path="../qi-ta-bu-chong.html">
  69. <a href="../qi-ta-bu-chong.html">
  70. 其他补充
  71. </a>
  72. <ul class="articles">
  73. <li class="chapter " data-level="1.3.1" data-path="param.html">
  74. <a href="param.html">
  75. Param
  76. </a>
  77. </li>
  78. <li class="chapter active" data-level="1.3.2" data-path="tablehelper.html">
  79. <a href="tablehelper.html">
  80. TableHelper
  81. </a>
  82. </li>
  83. <li class="chapter " data-level="1.3.3" data-path="ke-zhi-xing-jiao-ben.html">
  84. <a href="ke-zhi-xing-jiao-ben.html">
  85. 可执行脚本
  86. </a>
  87. </li>
  88. </ul>
  89. </li>
  90. <li class="divider"></li>
  91. <li>
  92. <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
  93. Published with GitBook
  94. </a>
  95. </li>
  96. </ul>
  97. </nav>
  98. </div>
  99. <div class="book-body">
  100. <div class="body-inner">
  101. <div class="book-header" role="navigation">
  102. <!-- Title -->
  103. <h1>
  104. <i class="fa fa-circle-o-notch fa-spin"></i>
  105. <a href=".." >TableHelper</a>
  106. </h1>
  107. </div>
  108. <div class="page-wrapper" tabindex="-1" role="main">
  109. <div class="page-inner">
  110. <div id="book-search-results">
  111. <div class="search-noresults">
  112. <section class="normal markdown-section">
  113. <h3 id="tablehelper">TableHelper</h3>
  114. <p><code>TableHelper</code>&#x662F;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;&#x8FDB;&#x884C;&#x7684;&#x5C01;&#x88C5;&#x3002;</p>
  115. <p>&#x4EE5;&#x4E0B;&#x793A;&#x4F8B;&#x6839;&#x636E;&#x6B64;&#x8868;&#x63A5;&#x53E3;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;</p>
  116. <pre><code class="lang-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">`test`</span> (
  117. <span class="hljs-string">`id`</span> <span class="hljs-built_in">bigint</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> AUTO_INCREMENT <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">&apos;id&apos;</span>,
  118. <span class="hljs-string">`nick`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">&apos;&#x6635;&#x79F0;&apos;</span>,
  119. <span class="hljs-string">`gender`</span> tinyint(<span class="hljs-number">1</span>) <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-string">&apos;1&apos;</span> <span class="hljs-keyword">COMMENT</span> <span class="hljs-string">&apos;&#x6027;&#x522B;&#xFF1A;1:&#x7537;,2:&#x5973;&apos;</span>,
  120. PRIMARY <span class="hljs-keyword">KEY</span> (<span class="hljs-string">`id`</span>)
  121. ) <span class="hljs-keyword">ENGINE</span>=<span class="hljs-keyword">InnoDB</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">CHARSET</span>=utf8mb4 <span class="hljs-keyword">COMMENT</span>=<span class="hljs-string">&apos;&#x6D4B;&#x8BD5;&#x8868;&apos;</span>;
  122. </code></pre>
  123. <h6 id="&#x67E5;&#x8BE2;">&#x67E5;&#x8BE2;</h6>
  124. <ul>
  125. <li>getAll($where, $keyword) &#x8FD4;&#x56DE;&#x591A;&#x6761;&#x67E5;&#x8BE2;&#x8BB0;&#x5F55;&#xFF0C;&#x4E8C;&#x7EF4;&#x6570;&#x7EC4;</li>
  126. <li>getRow($where, $keyword) &#x8FD4;&#x56DE;&#x4E00;&#x6761;&#x67E5;&#x8BE2;&#x8BB0;&#x5F55;&#xFF0C;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;</li>
  127. <li>getCount($where, $keyword) &#x8FD4;&#x56DE;&#x7B26;&#x5408;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#x7684;&#x8BB0;&#x5F55;&#x6570;&#x91CF;</li>
  128. <li>getOne($where, $keyword) &#x8FD4;&#x56DE;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x4E00;&#x884C;&#x7684;&#x7B2C;&#x4E00;&#x5217;&#x8BB0;&#x5F55;</li>
  129. <li>getCol($where, $keyword) &#x8FD4;&#x56DE;&#x5217;&#x8BB0;&#x5F55;</li>
  130. </ul>
  131. <p><code>$where</code>&#x662F;&#x786E;&#x5207;&#x503C;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#xFF0C;&#x6BD4;&#x5982;&#x4E3B;&#x952E;</p>
  132. <pre><code class="lang-php">$where = [
  133. <span class="hljs-string">&apos;id&apos;</span> =&gt; <span class="hljs-number">1</span>,
  134. ];
  135. <span class="hljs-comment">// or</span>
  136. $where = [
  137. <span class="hljs-string">&apos;nick&apos;</span> =&gt; <span class="hljs-string">&apos;solu&apos;</span>,
  138. <span class="hljs-string">&apos;gender&apos;</span> =&gt; <span class="hljs-number">1</span>,
  139. ];
  140. getRow($where);
  141. </code></pre>
  142. <p>&#x590D;&#x6742;&#x67E5;&#x8BE2;&#x53EF;&#x4EE5;&#x7EC4;&#x5408;<code>$keyword</code>&#x914D;&#x7F6E;&#x67E5;&#x8BE2;</p>
  143. <pre><code class="lang-php">$where = [
  144. ];
  145. $keyword = [
  146. <span class="hljs-string">&apos;_field&apos;</span> =&gt; <span class="hljs-string">&apos;id,nick&apos;</span>, <span class="hljs-comment">// &#x6307;&#x5B9A;&#x67E5;&#x8BE2;&#x5B57;&#x6BB5;</span>
  147. <span class="hljs-string">&apos;_where&apos;</span> =&gt; <span class="hljs-string">&quot;nick like &apos;so%&apos;&quot;</span>, <span class="hljs-comment">// &#x590D;&#x6742;where&#x6761;&#x4EF6;</span>
  148. <span class="hljs-string">&apos;_limit&apos;</span> =&gt; <span class="hljs-string">&apos;0,5&apos;</span>, <span class="hljs-comment">// &#x67E5;&#x8BE2;&#x6570;&#x91CF;</span>
  149. <span class="hljs-string">&apos;_sortKey&apos;</span> =&gt; <span class="hljs-string">&apos;id DESC&apos;</span>, <span class="hljs-comment">// &#x6392;&#x5E8F;</span>
  150. <span class="hljs-string">&apos;_groupby&apos;</span> =&gt; <span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-comment">// &#x5206;&#x7EC4;&#x64CD;&#x4F5C;</span>
  151. <span class="hljs-string">&apos;_debug&apos;</span> =&gt; <span class="hljs-keyword">true</span>, <span class="hljs-comment">// &#x6253;&#x5370;sql&#x8BED;&#x53E5;&#xFF0C;&#x4F1A;&#x7ED3;&#x675F;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;</span>
  152. ];
  153. getAll($where, $keyword);
  154. </code></pre>
  155. <h6 id="&#x63D2;&#x5165;">&#x63D2;&#x5165;</h6>
  156. <ul>
  157. <li>addObject($data) &#x63D2;&#x5165;&#x4E00;&#x6761;&#x6570;&#x636E;</li>
  158. <li>addObjects($cols, $datas) &#x63D2;&#x5165;&#x591A;&#x6761;&#x6570;&#x636E;&#xFF0C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x5B57;&#x6BB5;$cols</li>
  159. <li>addObjects2($datas) &#x63D2;&#x5165;&#x591A;&#x6761;&#x8BB0;&#x5F55;&#xFF0C;&#x8BFB;&#x53D6;&#x7B2C;&#x4E00;&#x6761;&#x8BB0;&#x5F55;&#x5B57;&#x6BB5;&#x540D;</li>
  160. <li>addObjectNx($data, $where) &#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x624D;&#x8FDB;&#x884C;&#x63D2;&#x5165;&#x64CD;&#x4F5C;</li>
  161. <li>replaceObject($data) replace into &#x63D2;&#x5165;&#x6570;&#x636E;</li>
  162. </ul>
  163. <pre><code class="lang-php">$data = [
  164. <span class="hljs-string">&apos;nick&apos;</span> =&gt; <span class="hljs-string">&apos;solu&apos;</span>,
  165. <span class="hljs-string">&apos;gender&apos;</span> =&gt; <span class="hljs-number">1</span>,
  166. ];
  167. addObject($data);
  168. $cols = [<span class="hljs-string">&apos;nick&apos;</span>, <span class="hljs-string">&apos;gender&apos;</span>];
  169. $datas = [
  170. [
  171. <span class="hljs-string">&apos;nick&apos;</span> =&gt; <span class="hljs-string">&apos;solu&apos;</span>,
  172. <span class="hljs-string">&apos;gender&apos;</span> =&gt; <span class="hljs-number">1</span>,
  173. ],
  174. [
  175. <span class="hljs-string">&apos;nick&apos;</span> =&gt; <span class="hljs-string">&apos;some&apos;</span>
  176. <span class="hljs-string">&apos;gender&apos;</span> =&gt; <span class="hljs-number">1</span>,
  177. ],
  178. ];
  179. addObjects($cols, $datas);
  180. <span class="hljs-comment">// or</span>
  181. addObjects2($datas);
  182. </code></pre>
  183. <h6 id="&#x5220;&#x9664;">&#x5220;&#x9664;</h6>
  184. <ul>
  185. <li>delObject($where) &#x5220;&#x9664;&#x8BB0;&#x5F55;</li>
  186. </ul>
  187. <pre><code class="lang-php">$where = [<span class="hljs-string">&apos;id&apos;</span> =&gt; <span class="hljs-number">1</span>];
  188. delObject($where);
  189. </code></pre>
  190. <h6 id="&#x66F4;&#x65B0;">&#x66F4;&#x65B0;</h6>
  191. <ul>
  192. <li>updateObject($data, $where) &#x66F4;&#x65B0;&#x64CD;&#x4F5C;</li>
  193. </ul>
  194. <pre><code class="lang-php">$where = [<span class="hljs-string">&apos;id&apos;</span> =&gt; <span class="hljs-number">1</span>];
  195. $data = [<span class="hljs-string">&apos;nick&apos;</span> =&gt; <span class="hljs-string">&apos;foobar&apos;</span>];
  196. updateObject($data, $where);
  197. </code></pre>
  198. <h6 id="redis2mysql">Redis2Mysql</h6>
  199. <p><code>Redis2Mysql</code>&#x662F;&#x5BF9;<code>TableHelper</code>&#x8FDB;&#x884C;&#x7F13;&#x5B58;&#x5C01;&#x88C5;&#xFF0C;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x4E0A;&#x57FA;&#x672C;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#x3002;</p>
  200. </section>
  201. </div>
  202. <div class="search-results">
  203. <div class="has-results">
  204. <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
  205. <ul class="search-results-list"></ul>
  206. </div>
  207. <div class="no-results">
  208. <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
  209. </div>
  210. </div>
  211. </div>
  212. </div>
  213. </div>
  214. </div>
  215. <a href="param.html" class="navigation navigation-prev " aria-label="Previous page: Param">
  216. <i class="fa fa-angle-left"></i>
  217. </a>
  218. <a href="ke-zhi-xing-jiao-ben.html" class="navigation navigation-next " aria-label="Next page: 可执行脚本">
  219. <i class="fa fa-angle-right"></i>
  220. </a>
  221. </div>
  222. <script>
  223. var gitbook = gitbook || [];
  224. gitbook.push(function() {
  225. gitbook.page.hasChanged({"page":{"title":"TableHelper","level":"1.3.2","depth":2,"next":{"title":"可执行脚本","level":"1.3.3","depth":2,"path":"qi-ta-bu-chong/ke-zhi-xing-jiao-ben.md","ref":"qi-ta-bu-chong/ke-zhi-xing-jiao-ben.md","articles":[]},"previous":{"title":"Param","level":"1.3.1","depth":2,"path":"qi-ta-bu-chong/param.md","ref":"qi-ta-bu-chong/param.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"qi-ta-bu-chong/tablehelper.md","mtime":"2018-09-08T08:48:07.550Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-13T03:13:15.790Z"},"basePath":"..","book":{"language":""}});
  226. });
  227. </script>
  228. </div>
  229. <script src="../gitbook/gitbook.js"></script>
  230. <script src="../gitbook/theme.js"></script>
  231. <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
  232. <script src="../gitbook/gitbook-plugin-search/search.js"></script>
  233. <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
  234. <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
  235. <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
  236. <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
  237. </body>
  238. </html>