thinkphp6 with
文章描述:
thinkphp6 with、append、hidden
模型关联
// 关联用户
public function user()
{
return $this->hasOne(UserModel::class, 'user_id', 'user_id');
}
// 获取用户昵称
public function getNicknameAttr()
{
return $this['user']['nickname'] ?? '';
}
// 获取用户账号
public function getUsernameAttr()
{
return $this['user']['username'] ?? '';
}
// 关联菜单
public function menu()
{
return $this->hasOne(MenuModel::class, 'menu_id', 'menu_id');
}
// 获取菜单名称
public function getMenuNameAttr()
{
return $this['menu']['menu_name'] ?? '';
}
// 获取菜单链接
public function getMenuUrlAttr($value, $data)
{
return $this['menu']['menu_url'] ?? $data['request_url'] ?? '';
}
$with = $append = $hidden = $field_no = [];
if (strpos($field, 'user_id') !== false) {
$with[] = $hidden[] = 'user';
$append[] = 'nickname';
$append[] = 'username';
}
if (strpos($field, 'menu_id') !== false) {
$with[] = $hidden[] = 'menu';
$append[] = 'menu_name';
$append[] = 'menu_url';
}
print_r($with);
Array
(
[0] => user
[1] => menu
)
print_r($append);
Array
(
[0] => nickname
[1] => username
[2] => menu_name
[3] => menu_url
)
print_r($hidden);
Array
(
[0] => user
[1] => menu
)
原查询
$list = $model->field($field)->where($where)->with($with)->append($append)->hidden($hidden)->order($order)->select()->toArray();
// ->with(['user','menu'])
// ->append(['nickname','username','menu_name','menu_url'])
// ->hidden(['user','menu'])
结果:
Array
(
[0] => Array
(
[log_id] => 1455
[user_id] => 1
[menu_id] => 41
[request_url] => admin/system.UserLog/list
[request_method] => GET
[request_ip] => 127.0.0.1
[request_region] => 内网IP
[request_isp] => 内网IP
[response_code] => 200
[response_msg] => 操作成功
[create_time] => 2024-04-28 14:50:41
)
)
->with(['user','menu'])
// ->append(['nickname','username','menu_name','menu_url'])
// ->hidden(['user','menu'])
结果:
Array
(
[0] => Array
(
[log_id] => 1455
[user_id] => 1
[menu_id] => 41
[request_url] => admin/system.UserLog/list
[request_method] => GET
[request_ip] => 127.0.0.1
[request_region] => 内网IP
[request_isp] => 内网IP
[response_code] => 200
[response_msg] => 操作成功
[create_time] => 2024-04-28 14:50:41
[user] => Array
(
[user_id] => 1
[avatar_id] => 0
[number] => 000
[nickname] => skyselang
[username] => skyselang
[password] => $2y$10$ArjnW.AXJjya2OEQIU/BjuFfLNW090zRv8cuI2O6ATKPpn2ml0cHK
[phone] =>
[email] =>
[remark] =>
[sort] => 200
[is_super] => 0
[is_disable] => 0
[is_delete] => 0
[login_num] => 51
[login_ip] => 127.0.0.1
[login_region] => 内网IP
[login_time] => 2024-04-28 14:48:18
[logout_time] => 2024-04-07 23:01:16
[create_uid] => 0
[update_uid] => 0
[delete_uid] => 0
[create_time] =>
[update_time] =>
[delete_time] =>
)
[menu] => Array
(
[menu_id] => 41
[menu_pid] => 53
[menu_type] => 1
[menu_name] => 用户日志
[menu_url] => admin/system.UserLog/list
[path] => user-log
[name] => SystemUserLog
[component] => system/user-log
[meta_icon] => el-icon-notebook-2
[meta_query] =>
[hidden] => 0
[sort] => 675
[is_unlogin] => 0
[is_unauth] => 0
[is_unrate] => 0
[is_disable] => 0
[is_delete] => 0
[create_uid] => 0
[update_uid] => 0
[delete_uid] => 0
[create_time] =>
[update_time] =>
[delete_time] =>
)
)
)
->with(['user','menu'])
->append(['nickname','username','menu_name','menu_url'])
// ->hidden(['user','menu'])
结果:
Array
(
[0] => Array
(
[nickname] => skyselang
[username] => skyselang
[menu_name] => 用户日志
[menu_url] => admin/system.UserLog/list
[log_id] => 1455
[user_id] => 1
[menu_id] => 41
[request_url] => admin/system.UserLog/list
[request_method] => GET
[request_ip] => 127.0.0.1
[request_region] => 内网IP
[request_isp] => 内网IP
[response_code] => 200
[response_msg] => 操作成功
[create_time] => 2024-04-28 14:50:41
[user] => Array
(
[user_id] => 1
[avatar_id] => 0
[number] => 000
[nickname] => skyselang
[username] => skyselang
[password] => $2y$10$ArjnW.AXJjya2OEQIU/BjuFfLNW090zRv8cuI2O6ATKPpn2ml0cHK
[phone] =>
[email] =>
[remark] =>
[sort] => 200
[is_super] => 0
[is_disable] => 0
[is_delete] => 0
[login_num] => 51
[login_ip] => 127.0.0.1
[login_region] => 内网IP
[login_time] => 2024-04-28 14:48:18
[logout_time] => 2024-04-07 23:01:16
[create_uid] => 0
[update_uid] => 0
[delete_uid] => 0
[create_time] =>
[update_time] =>
[delete_time] =>
)
[menu] => Array
(
[menu_id] => 41
[menu_pid] => 53
[menu_type] => 1
[menu_name] => 用户日志
[menu_url] => admin/system.UserLog/list
[path] => user-log
[name] => SystemUserLog
[component] => system/user-log
[meta_icon] => el-icon-notebook-2
[meta_query] =>
[hidden] => 0
[sort] => 675
[is_unlogin] => 0
[is_unauth] => 0
[is_unrate] => 0
[is_disable] => 0
[is_delete] => 0
[create_uid] => 0
[update_uid] => 0
[delete_uid] => 0
[create_time] =>
[update_time] =>
[delete_time] =>
)
)
)
->with(['user','menu'])
->append(['nickname','username','menu_name','menu_url'])
->hidden(['user','menu'])
结果:
Array
(
[0] => Array
(
[nickname] => skyselang
[username] => skyselang
[menu_name] => 用户日志
[menu_url] => admin/system.UserLog/list
[log_id] => 1455
[user_id] => 1
[menu_id] => 41
[request_url] => admin/system.UserLog/list
[request_method] => GET
[request_ip] => 127.0.0.1
[request_region] => 内网IP
[request_isp] => 内网IP
[response_code] => 200
[response_msg] => 操作成功
[create_time] => 2024-04-28 14:50:41
)
)
//使用hasWhere根据article的条件查询(注:comment与article有关联),同时使用with把article查询出来:
public function demo5(){
//方法一
$list = Comment::hasWhere('article',[["title","like","%美国%"]])->with("article")->select()->toArray();
//方法二
$list = Comment::with(['article'=>function($query){
$query->where("title","like","%量子%");
}])->select();
halt($list);
}
在TP6中当我们使用hasWhere方法时,如果附表的字段和主表的字段重名,而我们又恰巧需要使用这个重名的字段来作为筛选条件时,就会报错。
$wherex[] = array('UserLogModel.is_delete','=',0);
$wherex[] = array('UserLogModel.log_id','in',[1339]);
$list = UserLogModel::field('*')
->hasWhere('menu',[["menu_name","like","%总数统计%"]])
// ->where([
// 'UserLogModel.is_delete' => 0, //此处的前缀必须是模型名 UserLogModel 或 表全称 lbs_system_user_log
// ])
->where($wherex)
->append(['nickname','username','menu_name','menu_url'])
->hidden(['user','menu'])
->order($order)
->with(['user','menu'])
->select()->toArray();
发布时间:2024/05/09
发表评论