🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### 1.添加一条数据 1)实例化对象赋值 并保存 $user = new User; $user->name='zhangsan'; $user->email='123@qq.com'; $user->save(); 2)使用data方法批量赋值 $user = new User(); $user->data([ 'name'=>'zhangsan', 'email'=>'123@qqc.om' ]); $user->save(); 3)实例化的时候直接传入数据 $user = new User([ 'name'=>'zhangsan', 'age'=>18 ]); $user->save(); 4)接收表单信息添加,过滤表中没有的字段 $user = new User($_POST); //过滤掉表中没有的字段 $user->allowField(true)->save(); 5)限制只写入的字段 $user = new User($_POST); //post数组中只有name和age会写入 $user->allowField(['name','age'])->save(); 6)获取添加该条数据的id $user = new User; $user->name = 'zhangsan'; $user->age = 18; $user->save(); //获取自增id echo $user->id; #### 2.添加多条数据 1)批量新增 $user = new User; $list = [ ['name'=>'zhnagsan','age'=>18], ['name'=>'lisi','age'=>19] ]; $user->saveAll($list); 2)静态方法,调用create方法创建并写入 $user = User::create([ 'name'=>'zhangsan', 'age'=>18 ]); echo $user->name; echo $user->age; echo $user->id; 3)助手函数 //使用model 实例化user模型 $user = model('User); //模型对象赋值 $user->data([ 'name'=>'zhangsan', 'age'=>1 ]); $user->save(); //助手函数批量新增 $user = model('User'); //批量新增 $list = [ ['name'=>'zhangsan','age'=>18], ['name'=>'lisi','age'=>19] ]; $user->saveAll($list);