💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### 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);