thinkphp 模型的curd

1.新增

  1.1  典型做法   

$user = new User;
// 过滤post数组中的非数据表字段数据   ,返回的受影响的行数
$data = Request::only(['name','email']);
$user->save($data);
//获取主键
$user->id


$user = new User;
// post数组中只有name和email字段会写入  
//使用allowField来指定允许的字段 true为表中的字段
$user->allowField(['name','email'])->save($_POST);

 1.2 使用静态方法

   

$user = User::create([
    'name'  =>  'thinkphp',
    'email' =>  '[email protected]'
]);
echo $user->name;
echo $user->email;
echo $user->id; // 获取自增ID
和save方法不同的是,create方法返回的是当前模型的对象实例。

 //create方法的第二个参数可以传入允许写入的字段列表(传入true则表示仅允许写入数据表定义的字段数据),

 2 修改

猜你喜欢

转载自www.cnblogs.com/mofei12138/p/11994871.html
今日推荐