ThinkPHP3.2订单插入错误捕获方法

      发布在:前端技术      评论:0 条评论
<p style="font-size: 16.002px; line-height: var(--ds-md-line-height); color: rgb(64, 64, 64); font-family: DeepSeek-CJK-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Noto Sans&quot;, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, Oxygen, &quot;Open Sans&quot;, sans-serif; text-wrap-mode: wrap; margin-top: 0px !important;">在ThinkPHP3.2中捕获订单数据插入错误,可以通过以下步骤实现:</p><h3 style="font-weight: var(--ds-font-weight-strong); font-size: calc(var(--ds-md-zoom)*16px); line-height: 1.5; margin: calc(var(--ds-md-zoom)*16px)0 calc(var(--ds-md-zoom)*12px)0; color: rgb(64, 64, 64); font-family: DeepSeek-CJK-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Noto Sans&quot;, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, Oxygen, &quot;Open Sans&quot;, sans-serif; text-wrap-mode: wrap;">步骤说明</h3><ol style="margin: calc(var(--ds-md-zoom)*12px)0; padding-left: calc(var(--ds-md-zoom)*24px); color: rgb(64, 64, 64); font-family: DeepSeek-CJK-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Noto Sans&quot;, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, Oxygen, &quot;Open Sans&quot;, sans-serif; font-size: 16.002px; text-wrap-mode: wrap;" class=" list-paddingleft-2"><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><strong>实例化模型</strong>:使用<code style="font-size: 0.875em; font-weight: var(--ds-font-weight-strong); font-family: var(--ds-font-family-code); border-radius: 4px; padding: 0.15rem 0.3rem;">M()</code>函数快速实例化订单模型。</p></li><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><strong>准备数据</strong>:构造要插入的订单数据数组。</p></li><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><strong>插入数据</strong>:调用<code style="font-size: 0.875em; font-weight: var(--ds-font-weight-strong); font-family: var(--ds-font-family-code); border-radius: 4px; padding: 0.15rem 0.3rem;">add()</code>方法插入数据,并获取返回值。</p></li><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><strong>错误判断</strong>:根据返回值判断是否插入成功,失败时获取详细错误信息。</p></li><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><strong>处理错误</strong>:记录日志或返回错误信息,确保生产环境不暴露敏感信息。</p></li></ol><h3 style="font-weight: var(--ds-font-weight-strong); font-size: calc(var(--ds-md-zoom)*16px); line-height: 1.5; margin: calc(var(--ds-md-zoom)*16px)0 calc(var(--ds-md-zoom)*12px)0; color: rgb(64, 64, 64); font-family: DeepSeek-CJK-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Noto Sans&quot;, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, Oxygen, &quot;Open Sans&quot;, sans-serif; text-wrap-mode: wrap;">示例代码</h3><pre class="brush:php;toolbar:false">//&nbsp;实例化订单模型 $Order&nbsp;=&nbsp;M(&#39;Order&#39;); //&nbsp;构造订单数据 $data&nbsp;=&nbsp;array( &nbsp;&nbsp;&nbsp;&nbsp;&#39;order_no&#39;&nbsp;=&gt;&nbsp;&#39;20231001123456&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&#39;user_id&#39;&nbsp;&nbsp;=&gt;&nbsp;1001, &nbsp;&nbsp;&nbsp;&nbsp;&#39;amount&#39;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;99.99, &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;其他字段... ); //&nbsp;插入数据 $result&nbsp;=&nbsp;$Order-&gt;add($data); if&nbsp;($result)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;插入成功,$result为插入的主键ID &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;订单创建成功,订单ID:&#39;&nbsp;.&nbsp;$result; }&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;插入失败,获取模型错误信息 &nbsp;&nbsp;&nbsp;&nbsp;$error&nbsp;=&nbsp;$Order-&gt;getError(); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;或获取数据库错误信息(更详细) &nbsp;&nbsp;&nbsp;&nbsp;$dbError&nbsp;=&nbsp;$Order-&gt;getDbError(); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;记录错误日志(实际开发中建议使用日志函数) &nbsp;&nbsp;&nbsp;&nbsp;Log::write(&#39;订单插入失败:&#39;&nbsp;.&nbsp;$dbError,&nbsp;&#39;ERROR&#39;); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;返回错误信息(生产环境应避免直接输出) &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;订单创建失败,错误原因:&#39;&nbsp;.&nbsp;$dbError; }</pre><h3 style="font-weight: var(--ds-font-weight-strong); font-size: calc(var(--ds-md-zoom)*16px); line-height: 1.5; margin: calc(var(--ds-md-zoom)*16px)0 calc(var(--ds-md-zoom)*12px)0; color: rgb(64, 64, 64); font-family: DeepSeek-CJK-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Noto Sans&quot;, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, Oxygen, &quot;Open Sans&quot;, sans-serif; text-wrap-mode: wrap;">关键点解析</h3><ul style="margin: calc(var(--ds-md-zoom)*12px)0; padding-left: calc(var(--ds-md-zoom)*24px); color: rgb(64, 64, 64); font-family: DeepSeek-CJK-patch, Inter, system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Noto Sans&quot;, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, Oxygen, &quot;Open Sans&quot;, sans-serif; font-size: 16.002px; text-wrap-mode: wrap;" class=" list-paddingleft-2"><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><strong>返回值判断</strong>:<code style="font-size: 0.875em; font-weight: var(--ds-font-weight-strong); font-family: var(--ds-font-family-code); border-radius: 4px; padding: 0.15rem 0.3rem;">add()</code>成功返回主键ID,失败返回<code style="font-size: 0.875em; font-weight: var(--ds-font-weight-strong); font-family: var(--ds-font-family-code); border-radius: 4px; padding: 0.15rem 0.3rem;">false</code>,据此判断是否插入成功。</p></li><li><p style="margin-top: 0px; margin-bottom: 4px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height);"><strong>错误信息获取</strong>:</p></li><ul style="list-style-type: square;" class=" list-paddingleft-2"><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><code style="font-size: 0.875em; font-weight: var(--ds-font-weight-strong); font-family: var(--ds-font-family-code); border-radius: 4px; padding: 0.15rem 0.3rem;">getError()</code>:获取模型层的错误信息(如自动验证失败)。</p></li><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><code style="font-size: 0.875em; font-weight: var(--ds-font-weight-strong); font-family: var(--ds-font-family-code); border-radius: 4px; padding: 0.15rem 0.3rem;">getDbError()</code>:获取数据库层面的详细错误(如SQL语法错误、约束冲突)。</p></li></ul><li><p style="margin-top: 0px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height); margin-bottom: 0px !important;"><strong>日志记录</strong>:使用<code style="font-size: 0.875em; font-weight: var(--ds-font-weight-strong); font-family: var(--ds-font-family-code); border-radius: 4px; padding: 0.15rem 0.3rem;">Log::write()</code>记录错误,便于后续排查,避免用户看到敏感信息。</p></li><li><p style="margin-top: 0px; margin-bottom: 4px; font-size: var(--ds-md-font-size); line-height: var(--ds-md-line-height);"><strong>事务处理(可选)</strong>:若涉及多表操作,可加入事务确保数据一致性:</p></li></ul><pre class="brush:php;toolbar:false">$Order-&gt;startTrans();&nbsp;//&nbsp;开启事务 try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;插入订单 &nbsp;&nbsp;&nbsp;&nbsp;$orderId&nbsp;=&nbsp;$Order-&gt;add($data); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!$orderId)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;Exception(&#39;订单插入失败&#39;); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;其他操作(如更新库存) &nbsp;&nbsp;&nbsp;&nbsp;$Inventory&nbsp;=&nbsp;M(&#39;Inventory&#39;); &nbsp;&nbsp;&nbsp;&nbsp;$updateResult&nbsp;=&nbsp;$Inventory-&gt;where(&#39;product_id=1&#39;)-&gt;setDec(&#39;stock&#39;); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!$updateResult)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;Exception(&#39;库存更新失败&#39;); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$Order-&gt;commit();&nbsp;//&nbsp;提交事务 &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;操作成功&#39;; }&nbsp;catch&nbsp;(Exception&nbsp;$e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$Order-&gt;rollback();&nbsp;//&nbsp;回滚事务 &nbsp;&nbsp;&nbsp;&nbsp;Log::write(&#39;事务失败:&#39;&nbsp;.&nbsp;$e-&gt;getMessage(),&nbsp;&#39;ERROR&#39;); &nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#39;操作失败:&#39;&nbsp;.&nbsp;$e-&gt;getMessage(); }</pre><p></p>
相关文章
热门推荐