<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                [TOC] ## setField 更新字段值 ### 1、功能:更新一個或多個字段 >[warning] 官方手冊:只提供一個更新一個字段的方法 查看源碼發現,其實將參數換成數組,可以同時更新多個字段 ### 2、源碼:/thinkphp/library/think/db/Query.php中的 setField方法 ![](https://box.kancloud.cn/a24dc3558f91fd95123906f8da9830a3_1236x596.png) ### 3、參數與返回值 | 序號 | 輸入參數 | 返回值 | | --- | --- | --- | | 1 | 2個參數時('字段名','字段值')<br/>1個參數時(與字段名對應的數組)| 受影響記錄條數<br/>即更新數量 | ### 4、語法(單條更新與多條更新): #### 一、更新單條記錄(必須是主鍵) >[info] 以單條記錄為例,下面給出常用的四種用法: * 更新一個字段,前面必須設置更新條件,如where,參數必須是二個字符串: ~~~ Db::table( 完整表名) -> where(更新條件) -> setField('字段名' , '字段值'); // 例如: Db::table('tp5_staff') -> where('id',1024) -> setField('salary' , 8500); ~~~ * 多字段更新,數據放在一個數組中傳入(主鍵在數組中): ~~~ // 1.創建員工信息數組 $data = []; $data[id] = 主鍵值; //設置更新條件 $data[ 字段1 ] = 字段值1 ; $data[ 字段2 ] = 字段值2 ; ...... //執行更新操作 Db::table( 表名 ) -> setField($data); ~~~ * 多字段更新,數據放在一個數組中(主鍵不在數組中),方法前必須給出更新條件: ~~~ // 1.創建員工信息數組 $data = []; $data[ 字段1 ] = 字段值1 ; $data[ 字段2 ] = 字段值2 ; ...... //執行更新操作 Db::table( 表名 ) -> where( 更新條件 ) -> setField($data); ~~~ * 多字段更新時,直接將主鍵,更新數組全部做為參數,一次性傳入: ~~~ //將更新條件與數據一次性傳入 Db::table( 表名 )->setField(['主鍵字段'=> 主鍵值,'字段1'=>字段值1,'字段2'=>字段值2,...]); //如果更新主鍵不放在參數數組中,則在方法前添加where方法 Db::table( 表名 )->where( 更新條件 ) -> setField(['字段1'=>字段值1,'字段2'=>字段值2,...]); ~~~ #### 二、同時更新多條記錄 (必須是條件表達式) >[info] 手冊上,并沒有給出同時更新多條記錄的方法,其實更新多條記錄也很簡單,只要在更新方法前,設置好更新條件即可。 * 基本語法: ~~~ //如果更新主鍵不放在參數數組中,則在方法前添加where方法 Db::table( 表名 )->where( 更新條件 ) -> setField(['字段1'=>字段值1,'字段2'=>字段值2,...]); //例如,將表中員工的工資小于3000元的,加薪500元,福利好吧? Db::table('tp5_staff' )->where( 'salary < 3000' ) -> setField( [ 'salary' => [ 'exp', 'salary + 500' ] ] ); ~~~ >[info] 可能有同學對: [ 'salary' => [ 'exp', 'salary + 500' ] ] ) 寫法不能理解,隨著后面對查詢表達式的學習,就理解了,這里僅僅知道可以同時更新多條記錄即可。 * * * * * ### 5、實例演示: >[info] 先查看一下當前tp5_staff表中數據 ![](https://box.kancloud.cn/070dfa93586fa525249d4d11b3007e56_570x326.png) #### 任務1:將id=1007的員工的工資salary 更新為8567 ,完成單字段更新 * Index.php 控制器代碼如下: ~~~ <?php namespace app\index\controller; use think\Db; class Index { public function index(){ // 1.更新數據 Db::table('tp5_staff') -> where('id',1007) -> setField('salary',8567); // 2.查看更新結果 dump(Db::table('tp5_staff')->find(1007)); } } ~~~ * 運行結果: ~~~ array(7) { ["id"] => int(1007) ["name"] => string(9) "潘金蓮" ["sex"] => int(0) ["age"] => int(39) ["salary"] => float(8567) ["dept"] => int(3) ["hiredate"] => string(10) "2016-03-20" } ~~~ * 表中id = 1007 的記錄已更新 ![](https://box.kancloud.cn/73ebdb3e130e9b8dc288d7cac41ee313_652x268.png) #### 任務2:將id=1011的員工的年齡、工資,入職日期修改,完成多字段更新 * 我們先查看一下李團長現在的情況: ~~~ <?php namespace app\index\controller; use think\Db; class Index { public function index(){ dump(Db::table('tp5_staff')->find(1011)); } } ~~~ * 更新前:李云龍信息如下: ~~~ array(7) { ["id"] => int(1011) ["name"] => string(9) "李云龍" ["sex"] => int(1) ["age"] => int(39) ["salary"] => float(4800) ["dept"] => int(1) ["hiredate"] => string(10) "2011-09-12" } ~~~ * 現在我們修改一下Index.php: ~~~ <?php namespace app\index\controller; use think\Db; class Index { public function index(){ //1.創建要更新的數據 $data = []; $data['id'] = 1011; $data['age'] = 49; $data['salary'] = 9850; $data['hiredate'] = '2005-10-20'; // 2.更新數據 Db::table('tp5_staff') -> setField($data); // 3.查看更新結果 dump(Db::table('tp5_staff')->find(1011)); } } ~~~ * 再次運行,查看結果: ~~~ array(7) { ["id"] => int(1011) ["name"] => string(9) "李云龍" ["sex"] => int(1) ["age"] => int(49) ["salary"] => float(9850) ["dept"] => int(1) ["hiredate"] => string(10) "2005-10-20" } ~~~ * 此時,查看數據表: ![](https://box.kancloud.cn/dd5080f0e89a4e488030d71b74c86498_607x308.png) #### 任務3 :將tp_staff表中年齡在20到30歲之間員工,工資加1000,入職時間統一修改為:2012-12-12 >[danger] 注意:該操作涉及多記錄更新,與前面實例不同 * 更新前表中數據: ![](https://box.kancloud.cn/4a6d0fc17d10c9d8f291a980e51fcc99_674x327.png) * Index.php 控制器代碼如下: ~~~ <?php namespace app\index\controller; use think\Db; class Index { public function index(){ //1.創建要更新條件 $map['age'] = ['between',[20,30]]; //2.創建更新數據 $data = []; $data['dept'] = 3; $data['salary'] = ['exp','salary + 1000']; $data['hiredate'] = '2012-12-12'; // 2.更新數據 Db::table('tp5_staff') -> where($map) -> setField($data); // 3.查看更新結果 dump(Db::table('tp5_staff')->where($map)->order('age')->select()); } } ~~~ * 運行結果(4條記錄受影響): ~~~ array(4) { [0] => array(7) { ["id"] => int(1001) ["name"] => string(6) "郭靖" ["sex"] => int(0) ["age"] => int(22) ["salary"] => float(6679) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } [1] => array(7) { ["id"] => int(1025) ["name"] => string(9) "小鈴鐺" ["sex"] => int(0) ["age"] => int(22) ["salary"] => float(6739) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } [2] => array(7) { ["id"] => int(1020) ["name"] => string(6) "虛竹" ["sex"] => int(0) ["age"] => int(28) ["salary"] => float(5765) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } [3] => array(7) { ["id"] => int(1002) ["name"] => string(9) "洪七公" ["sex"] => int(0) ["age"] => int(29) ["salary"] => float(5365) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } } ~~~ * 此時,表中數據如下: ![](https://box.kancloud.cn/c612c659a725967ecc8fa28ae7c1e719_1056x783.png) ### 6、總結 >[success] 1.update方法與setField方法都可以完成同樣的工作; 2.日常開發中,推薦使用update方法; 3.當僅更新單條記錄中某個字段值時,用setField方法更簡潔和直觀。 ### 7、作業 >[info] setField方法的若干用法,建議每個都上機操作一遍~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看