笔记tp5:tp5模型操作

笔记tp5:tp5模型操作

1.增://只是选好用的记录
1.增加一条:

$user = new User([
    'name'  =>  'thinkphp',
    'email' =>  '[email protected]'
]);
$user->save();

2.增加多条:

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

静态方法:

$user = User::create([
    'name'  =>  'thinkphp',
    'email' =>  '[email protected]'
]);

2.删://注意delete和destroy的调用方式
1.实例化删除

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

2.根据主键删除

User::destroy(1);
User::destroy('1,2,3');User::destroy([1,2,3]);

3.条件删除

User::destroy(['status' => 0]);
User::where('id','>',10)->delete();

3.改/更新:
1.直接更新或 查找更新

$user = User::get(1);
$user = new User;
$user->save([// save方法第二个参数为更新条件
    'name'  => 'thinkphp',
    'email' => '[email protected]'
],['id' => 1]);

2.通过数据库 实例

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

3.静态方法

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

4.查:
1.查单个数据

$user = User::get(1);
$user = User::get(['name' => 'thinkphp']);
$user = new User();$user->where('name', 'thinkphp')->find();

2.查多个数据

$list = User::all('1,2,3');
$user = new User();
$user->where('name', 'thinkphp') ->limit(10) ->order('id', 'desc')->select();
foreach($list as $key=>$user){
    echo $user->name;
}

猜你喜欢

转载自blog.csdn.net/m0_49888984/article/details/108045799
tp5