目录
7:group用法(订单表,同一个人可以下多个订单,共有几个人下单)
1:注意事情
当新增数据用save的时候 数据表中:create_time、update_time可以不用写,会制动生成
2:连表查询
join :被关联的数据表
alias:给原表起别名,被关联的表其别名直接空格后面写上别名
as: 两个字段操作后其别名(真实浏览量+固定值 as 字段),距离如下
$where['ktgy_article.article_status'] = 1; // 1:显示 2:隐藏
$where['ktgy_article.is_delete'] = 0; // 0:没删除
$where['ktgy_article.wxapp_id'] = $wxapp_id;
$where['ktgy_article.category_id'] = $category_id; //分类id
try {
$res = DB::table('ktgy_article')
->alias('a')
->join('ktgy_upload_file f','a.image_id = f.file_id')
->field('a.article_id,a.article_title,a.create_time,a.virtual_views,a.actual_views,a.virtual_views+a.actual_views as sum_views,a.article_sort,f.file_name,f.file_url,f.storage')
->where($where)
->order(['a.article_sort' => 'asc'])
->paginate($limit, false, [
'query' => \request()->request()
]);
} catch (\Exception $e) {
$this->error = $e->getMessage();
return false;
}
3:像状态修改这类,要先给状态先复赋值给一个新的变量
public function updatestatus($article_id,$article_status)
{
$status = $article_status;
if ($status == 1){
$article_status = 2;
}
if ($status == 2){
$article_status = 1;
}
return $this->update(['article_id' => $article_id, "article_status" => $article_status]);
}
错误如下
public function updatestatus($article_id,$article_status)
{
if ($article_status== 1){
$article_status = 2;
}
if ($article_status== 2){
$article_status = 1;
}
return $this->update(['article_id' => $article_id, "article_status" => $article_status]);
}
4: with的用法
如果查关联表所有数据 model层 如下
public function getList()
{
$where['wxapp_id'] = self::$wxapp_id;
try {
$info = $this
->with(['image'])
->where($where)
->paginate(15, false, [
'query' => request()->request()
]);
} catch (\Exception $e) {
$this->error = $e->getMessage();
return false;
}
return $info;
}
如果查关联表部分数据 model层 如下
public function getList()
{
$where['wxapp_id'] = self::$wxapp_id;
try {
$info = $this
->with(['image'=> function($query){
$query->withField('file_id,storage, file_name, file_url');
}])
->where($where)
->paginate(15, false, [
'query' => request()->request()
]);
} catch (\Exception $e) {
$this->error = $e->getMessage();
return false;
}
return $info;
}
其中的 image 方法如下
public function image()
{
return $this->hasOne('uploadFile', 'file_id', 'images_id');
}
5:修改数据 先查询在修改
/**
* 编辑广告
* @param $article_images_id
* @return array|bool|mixed
*/
public function imagesEdit($article_images_id)
{
// 文章详情
$model = ArticleImagesModel::detail($article_images_id);
if (!$this->request->isAjax()) {
// 文章分类
return $this->fetch('imagesEdit', compact('model'));
}
// 更新记录
if ($model->imagesEdit($this->postData('article'))) {
return $this->renderSuccess('更新成功', url('content.article/imagesIndex'));
}
return $this->renderError($model->getError() ?: '更新失败');
}
/**
* image 方法
* @return \think\model\relation\HasOne
*/
public function image()
{
return $this->hasOne('uploadFile', 'file_id', 'images_id');
}
/**
*查询数据
* @param $article_images_id
* @return ArticleImages|null
* @throws \think\exception\DbException
*/
public static function detail($article_images_id)
{
return self::get($article_images_id, ['image']);
}
/**
* 更新数据
* @param $data
* @return bool
*/
public function imagesEdit($data)
{
if (empty($data['images_id'])) {
$this->error = '请上传轮播图';
return false;
}
if (empty($data['link_url'])) {
$this->error = '请输入轮播图链接';
return false;
}
return $this->allowField(true)->save($data) !== false;
}
6: 一对多,并计算多的总和
注释:with 连表,paginate 分页, each 遍历,$acWhere查询条件,sprintf 金额经度的计算,sum计算某个字段的值
public function getList()
{
$where['wxapp_id'] = self::$wxapp_id;
return $info = $this->with(['image' => function($query){
$query->withField('file_id,storage, file_name, file_url');
}])
->where($where)
->paginate(15, false, [
'query' => request()->request()
])
->each(function(&$item, $key) {
//统计活动已捐款额
$orderActivityModel = new OrderActivity();
$acWhere = [
"to_id"=>$item["activity_id"],
'pay_status'=>2
];
$item["sum_price"] = sprintf("%.2f", $orderActivityModel->where($acWhere)->sum("pay_price"));
//判断活动状态
$time = date('Y-m-d h:i:s', time());
if ($item['activity_end'] >= $time && $item['activity_start'] <= $time){
$item['activity_status'] = config("lang.activity_status")[1];//进行中
}
if ($item['activity_end'] < $time){
$item['activity_status'] = config("lang.activity_status")[2];//已结束
}
if ($item['activity_start'] > $time){
$item['activity_status'] = config("lang.activity_status")[3];//未开始
}
return $item;
});
}
7:group用法(订单表,同一个人可以下多个订单,共有几个人下单)
public function onlineIndex()
{
//查活动
$where['wxapp_id'] = self::$wxapp_id;
$where['activity_type'] = 1;
$info = $this
->where($where)
->field('activity_id,activity_name,activity_start as start,activity_end as end,activity_end - activity_start as diff_time')
->paginate(15, false, [
'query' => request()->request()
]);
$model = new OrderActivityModel();
foreach ($info as $k=>$v) {
//统计活动的参与人数和资金
$map['to_id'] = $v['activity_id'];
$map['pay_status'] = 2;
$group_num = $model
->where($map)
->field('from_id,COUNT(*) as total')
->group('from_id')
->select();
$info[$k]['sum_people'] = count($group_num);
$info[$k]['sum_money'] = $bb = $model->where($map)->sum('pay_price');
//活动状态
$time = time();
if ($v['start'] > $time) {
$info[$k]['activity_status'] = '未进行';
}
if ($v['start'] <= $time && $time <= $v['end']) {
$info[$k]['activity_status'] = '进行中';
}
if ($time >= $v['end']) {
$info[$k]['activity_status'] = '已结束';
}
//剩余时间
$info[$k]['days'] = intval($v['diff_time']/86400);
$remain = $v['diff_time']%86400;
$info[$k]['hours'] = intval($remain/3600);
$remain = $remain%3600;
$info[$k]['mins'] = intval($remain/60);
}
return $info;
}