controller调用model层:
1. 最简单的方法:
public function testModel(){
$log = new Log;
$log->user_id = 123;
$log->ip = '10.10.10.10';
$log->save();
}
2. 数组的方法:
$logArr['user_id'] = 1123123;
$logArr['ip'] = '127.0.0.1';
if($result = Log::create($logArr))
echo " 添加成功";
3.批量新增
$log = new Log;
$list = [
['user_id' => 123, 'ip' => '192.168.1.100'],
['user_id' => 456, 'ip' => '192.168.1.101']
];
if($log->saveAll($list))
echo " 日志批量新增成功";
1. 查询一组语句
$log = Log::get(1);
echo $log['ip'];
2. 根据某个条件查询数据getByXxxx()方法
$log = Log::getByUserId('1');
echo $log['ip'];
3. 根据数组条件查询
$log = Log::get(['user_id'=>1,'ip'=>'1.1.1.1']);
//不怎么好使
$log = Log::where('user_id', 1)->find();
$log = Log::where(['user_id'=>1,'ip'=>'1.1.1.1'])->find();
4. 查询多个数据
$log = Log::all();
$log = Log::all(['user_id'=>1]);
$log = Log::where('id', '<=', 3)->select();
print_r($log);
1.对数据查询出来的数据进行更新数据
$log = Log::get(1);
$log->user_id = 3;
$log->ip = '10.1.11.11';
if(false != $log->save())
return "更新用户成功";
else
return $log->getError();
2.自己定义是数据更新操作
$logArr['user_id'] = 666;
$logArr['ip'] = '10.10.11.11';
Log::update($logArr, ['id' => 2]);
$log = Log::get(146);
$log->delete();