- 查找方法
- get
- getLock
- find
- findCreate
- select
查找方法
get
/*** 根据主键获取对象* @param $id* @param bool $cache 是否使用缓存* @param null $cache_time 缓存时间* @return mixed|Record|string*/public static function get($id,$cache= true,$cache_time=null)
使用缓存时 获取相同 id 的对象会使用缓存
实例:
$user=User::get(1);$user->name='tengzhinei';$user->save();
getLock
获取数据同时加锁,注意需要在事物中使用
DB::runInTrans(function(){$user=User::getLock(1);$user->name='tengzhinei';$user->save();});
find
public static function find(array $where)
查询一条符合条件的记录,没有返回null
实例:
$user=User::find(['domain'=>'rapphp','account'=>'tengzhinei']);
如果模型中配置了缓存key且查询条件匹配的换查询结果会进缓存
class User extend Record{public function cacheKeys(){return ['domain,account'];}}
findCreate
public static function findCreate(array $where)
查询一条符合条件的记录,如果没有返回一条条件相同的对象
实例:
$user=User::findCreate(['domain'=>'rapphp','account'=>'tengzhinei']);如果数据库不存在当前对象返回的对象不为空返回对象 User{'domain':'rapphp','account':'tengzhinei'}
缓存条件和 find 相同
select
/*** 检索* @param string $fields 字段* @param bool $contain 包含还是不包含* @return Select*/public static function select($fields='', $contain=true)
实例
//返回全部字段$users=User::select()->limit(1,10)->findAll();//返回部分字段$users=User::select('name,account'')->limit(1,10)->findAll();//不包含字段$users=User::select('phone',false')->limit(1,10)->findAll();
上一篇:增删改 下一篇:数据类型
