TP5的select,find和get查询方法的使用

select查询出的是多条数据,需要在模版volist循环打印出来

find和get获取单条数据,可直接在模版打印;

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

// 使用数组查询
$user = User::get(['name' => 'thinkphp']);

// 使用闭包查询
$user = User::get(function($query){
    $query->where('name', 'thinkphp');
});
echo $user->name;

如果你是在模型内部,请不要使用$this->name的方式来获取数据,请使用$this->getAttr('name') 替代。

或者在实例化模型后调用查询方法

$user = new User();
// 查询单个数据
$user->where('name', 'thinkphp')
    ->find();

get或者find方法返回的是当前模型的对象实例,可以使用模型的方法。


$user = new User();
// 查询数据集
$user->where('name', 'thinkphp')
    ->limit(10)
    ->order('id', 'desc')
    ->select();

模型的all方法或者select方法返回的是一个包含模型对象的二维数组或者数据集对象。


猜你喜欢

转载自blog.csdn.net/honkkki/article/details/80644943
今日推荐