欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          Thinkphp框架對數(shù)據(jù)庫的操作(總結(jié))

          Thinkphp框架對數(shù)據(jù)庫的操作(總結(jié))

          1.添加數(shù)據(jù)

          1.1添加一條數(shù)據(jù)

          $user           = new User; $user->name     = 'thinkphp'; $user->email    = 'thinkphp@qq.com'; $user->save(); $user = new User; $user->save([     'name'  =>  'thinkphp',     'email' =>  'thinkphp@qq.com' ]);

          1.2過濾非數(shù)據(jù)表字段的數(shù)據(jù)

          $user = new User; // 過濾post數(shù)組中的非數(shù)據(jù)表字段數(shù)據(jù) $user->allowField(true)->save($_POST);

          1.3指定某些字段數(shù)據(jù)

          $user = new User; // post數(shù)組中只有name和email字段會寫入 $user->allowField(['name','email'])->save($_POST);

          1.4添加多條數(shù)據(jù)

          $user = new User; $list = [     ['name'=>'thinkphp','email'=>'thinkphp@qq.com'],     ['name'=>'onethink','email'=>'onethink@qq.com'] ]; $user->saveAll($list);

          1.5靜態(tài)方法

          $user = User::create([     'name'  =>  'thinkphp',     'email' =>  'thinkphp@qq.com' ]); echo $user->name; echo $user->email; echo $user->id; // 獲取自增ID

          2.更新數(shù)據(jù)

          2.1查找并更新

          $user = User::get(1); $user->name     = 'thinkphp'; $user->email    = 'thinkphp@qq.com'; $user->save();

          2.2直接更新數(shù)據(jù)

          $user = new User; // save方法第二個參數(shù)為更新條件 $user->save([     'name'  => 'thinkphp',     'email' => 'thinkphp@qq.com' ],['id' => 1]);

          2.3 過濾非數(shù)據(jù)表字段

          $user = new User; // 過濾post數(shù)組中的非數(shù)據(jù)表字段數(shù)據(jù) $user->allowField(true)->save($_POST,['id' => 1]);

          2.4指定某些字段

          $user = new User(); // post數(shù)組中只有name和email字段會寫入 $user->allowField(['name','email'])->save($_POST, ['id' => 1]);

          2.5批量更新數(shù)據(jù)

          $user = new User; $list = [     ['id'=>1, 'name'=>'thinkphp', 'email'=>'thinkphp@qq.com'],     ['id'=>2, 'name'=>'onethink', 'email'=>'onethink@qq.com'] ]; $user->saveAll($list);

          2.6靜態(tài)方法

          User::where('id', 1)     ->update(['name' => 'thinkphp']);

          2.7自動識別

          2.7.1顯示更新數(shù)據(jù)

          // 實例化模型 $user = new User; // 顯式指定更新數(shù)據(jù)操作 $user->isUpdate(true)     ->save(['id' => 1, 'name' => 'thinkphp']);

          2.7.2顯示新增數(shù)據(jù)

          $user = User::get(1); $user->name = 'thinkphp'; // 顯式指定當前操作為新增操作 $user->isUpdate(false)->save();

          3.刪除數(shù)據(jù)

          3.1刪除當前模型

          $user = User::get(1); $user->delete();

          3.2根據(jù)主鍵刪除

          User::destroy(1); // 支持批量刪除多個數(shù)據(jù) User::destroy('1,2,3'); // 或者 User::destroy([1,2,3]);

          3.3條件刪除

          User::destroy(function($query){     $query->where('id','>',10); });

          4.查詢數(shù)據(jù)

          4.1獲取單個數(shù)據(jù)

          // 取出主鍵為1的數(shù)據(jù) $user = User::get(1); echo $user->name; // 使用查詢構(gòu)造器查詢滿足條件的數(shù)據(jù) $user = User::where('name', 'thinkphp')->find(); echo $user->name;

          4.2獲取多個數(shù)據(jù)

          // 根據(jù)主鍵獲取多個數(shù)據(jù) $list = User::all('1,2,3'); // 或者使用數(shù)組 $list = User::all([1,2,3]); // 對數(shù)據(jù)集進行遍歷操作 foreach($list as $key=>$user){     echo $user->name; }
          // 使用查詢構(gòu)造器查詢 $list = User::where('status', 1)->limit(3)->order('id', 'asc')->select(); foreach($list as $key=>$user){     echo $user->name; }

          4.3獲取某個字段或者某個列的值

          // 獲取某個用戶的積分 User::where('id',10)->value('score'); // 獲取某個列的所有值 User::where('status',1)->column('name'); // 以id為索引 User::where('status',1)->column('name','id');

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號