//使用ActiveRecord方式查询
public function actionCurd()
{
$userModel = new $this->modelClass();
$info = $userModel::findOne(19);//查询单条数据
$followList = $info->getFollow()->orderBy('id')->all();//关联查询(一对多),查询用户对应多条关联数据(管理model如果没有其余操作,建议放在根目录common下)
// $list = $follow::findOne('75');
//数据添加
$info->nickName = '昵称1';//(info 之前只是new操作,添加数据;有查询;表示更新数据)
// $list->id = 75;
$info->save(false); //save(false)不验证rules
return $follow->errors;//返回更新错误信息
//
$arr = [];
foreach ($follow::find()->each(10) as $customers) {//批量查询数据,一次十条;多个数组
// $customers 是个最多拥有 10 条数据的数组
array_push($arr,$customers);
}
foreach ($follow::find()->batch(10) as $customers) {//批量查询数据,十条数据一组
// $customers 是个最多拥有 10 条数据的数组
array_push($arr,$customers);
}
return $arr;
//根据客户端传递数据,操作数据库
if ($model->load($data, '') && $model->save()) {
return '成功';
}else{
return '提供的数据格式不正确!';
}
if ($model->load(\Yii::$app->request->post()) && $model->save(false)) {
return ['code'=>1,'msg'=>'成功'];
}else{
return $model->errors;
return ['code'=>0,'msg'=>'失败'];
}
}
来源:oschina
链接:https://my.oschina.net/u/3358460/blog/3158042