有两个方法,
namespace app\index\model;
use think\Model;
class User extends Model
{
//如果表名和文件名不是对应的,用下面代码修改
protected $table = 'think_user';
}
>php think make:model index/Blog
有三种方式
use app\index\model\User;
$li= User::get(1);
use think\Loader;
$user= Loader::model('user');
$li= $user::get(1);
$user= model('User');
$li= $user::get(1);
get的意思是,查询主键为1的数据。后面是查询name=thinkphp的数据
//取出主键为1的数据
$user = User::get(1);
// 使用数组查询
$user = User::get(['name' => 'thinkphp']);
也可以使用系统查询方法
$user = new User();
$user->where('name', 'thinkphp')->find();
用all()方法
// 根据主键获取多个数据
$list = User::all('1,2,3');
// 或者使用数组
$list = User::all([1,2,3]);
foreach($list as $key=>$user){
echo $user->name;
}
// 使用数组查询
$list = User::all(['status'=>1]);
// 使用闭包查询
$list = User::all(function($query){
$query->where('status', 1)->limit(3)->order('id', 'asc');
});
foreach($list as $key=>$user){
echo $user->name;
}
或者用查询方法查看
$user = new User();
// 查询数据集
$user->where('name', 'thinkphp')
->limit(10)
->order('id', 'desc')
->select();
// 获取某个用户的积分
User::where('id',10)->value('score');
// 获取某个列的所有值
User::where('status',1)->column('name');
// 以id为索引
User::where('status',1)->column('name','id');
User::where('status',1)->column('id,name');
// 根据name字段查询用户
$user = User::getByName('thinkphp');
// 根据email字段查询用户
$user = User::getByEmail('[email protected]');
用save方法
$user = new User;
$user->name = 'thinkphp';
$user->email = '[email protected]';
$user->save();
用data批量方法保存
$user = new User;
$user->data([
'name' => 'thinkphp',
'email' => '[email protected]'
]);
$user->save();
//如果需要过滤非数据表字段的数据,可以使用:
$user = new User($_POST);
// 过滤post数组中的非数据表字段数据
$user->allowField(true)->save();
//如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:
$user = new User($_POST);
// post数组中只有name和email字段会写入
$user->allowField(['name','email'])->save();
$user = new User;
$user->name = 'thinkphp';
$user->email = '[email protected]';
$user->save();
// 获取自增ID
echo $user->user_id;
$user = new User;
$list = [
['name'=>'thinkphp','email'=>'[email protected]'],
['name'=>'onethink','email'=>'[email protected]']
];
$user->saveAll($list);
// 使用model助手函数实例化User模型
$user = model('User');
// 模型对象赋值
$user->data([
'name' => 'thinkphp',
'email' => '[email protected]'
]);
$user->save();
或者进行批量新增:
$user = model('User');
// 批量新增
$list = [
['name'=>'thinkphp','email'=>'[email protected]'],
['name'=>'onethink','email'=>'[email protected]']
];
$user->saveAll($list);
$user = User::get(1);
$user->delete();
User::destroy(1);
// 支持批量删除多个数据
User::destroy('1,2,3');
// 或者
User::destroy([1,2,3]);
// 删除状态为0的数据
User::destroy(['status' => 0]);
还支持使用闭包删除,例如:
User::destroy(function($query){
$query->where('id','>',10);
});
或者通过数据库类的查询条件删除
User::where('id','>',10)->delete();
$user = User::get(1);
$user->name = 'thinkphp';
$user->email = '[email protected]';
$user->save();
$user = new User;
// save方法第二个参数为更新条件
$user->save([
'name' => 'thinkphp',
'email' => '[email protected]'
],['id' => 1]);
$user = new User();
// 过滤post数组中的非数据表字段数据
$user->allowField(true)->save($_POST,['id' => 1]);
//如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:
$user = new User();
// post数组中只有name和email字段会写入
$user->allowField(['name','email'])->save($_POST, ['id' => 1]);
$user = new User;
$list = [
['id'=>1, 'name'=>'thinkphp', 'email'=>'[email protected]'],
['id'=>2, 'name'=>'onethink', 'email'=>'[email protected]']
];
$user->isUpdate()->saveAll($list);
$user = new User;
$user->where('id', 1)
->update(['name' => 'thinkphp']);
或者使用:
$user = new User;
$user->update(['id' => 1, 'name' => 'thinkphp']);