static function getList($params){
extract($params);
$where = [];
if(isset($uid)){
$where['offline_store_rights_order.uid'] = $uid;
}
$list = self
::with(['store'])
->where($where)->alias('offline_store_rights_order')
->order('offline_store_rights_order.id desc')
->paginate()->each(function($item){
$item->store->logoimage = cdnurl($item->store->logoimage, true);
return $item;
});
foreach ($list as $row){
$row->visible(['id','code','rights_description','rights_content','createtime','usetime','store_name','logoimage','address','lon','lat']);
$row->visible(['store']);
$row->getRelation('store')->visible(['store_name','logoimage','address','lon','lat']);
}
return $list;
}
public function store()
{
return $this->belongsTo('app\admin\model\shopro\OfflineStore', 'offline_store_id', 'id', [], 'LEFT')->setEagerlyType(0);
} |
static function getList($params){
extract($params);
$where = [];
if(isset($uid)){
$where['offline_store_rights_order.uid'] = $uid;
}
$list = self
::with(['store'])
->where($where)->alias('offline_store_rights_order')
->order('offline_store_rights_order.id desc')
->paginate()->each(function($item){
$item->store->logoimage = cdnurl($item->store->logoimage, true);
return $item;
});
foreach ($list as $row){
$row->visible(['id','code','rights_description','rights_content','createtime','usetime','store_name','logoimage','address','lon','lat']);
$row->visible(['store']);
$row->getRelation('store')->visible(['store_name','logoimage','address','lon','lat']);
}
return $list;
}
public function store()
{
return $this->belongsTo('app\admin\model\shopro\OfflineStore', 'offline_store_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
上述模型查询对应sql
[ sql ] [ DB ] CONNECT:[ UseTime:0.110203s ] mysql:host=116.62.184.212;dbname=shop_wanhedashuj;charset=utf8mb4
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_user_token` [ RunTime:0.073344s ]
[ sql ] [ SQL ] SELECT * FROM `pl_user_token` WHERE `token` = 'c1dcced465b5170236f038309ba79fd718b38d2d' LIMIT 1 [ RunTime:0.072282s ]
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_user` [ RunTime:0.073363s ]
[ sql ] [ SQL ] SELECT * FROM `pl_user` WHERE `id` = 83 LIMIT 1 [ RunTime:0.072761s ]
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_shopro_offline_store_rights_order` [ RunTime:0.072322s ]
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_shopro_offline_store` [ RunTime:0.072191s ]
[ sql ] [ SQL ] SELECT COUNT(*) AS tp_count FROM `pl_shopro_offline_store_rights_order` `offline_store_rights_order` LEFT JOIN `pl_shopro_offline_store` `store` ON `offline_store_rights_order`.`offline_store_id`=`store`.`id` WHERE `offline_store_rights_order`.`uid` = 83 LIMIT 1 [ RunTime:0.071331s ]
[ sql ] [ SQL ] SELECT `offline_store_rights_order`.`id`,`offline_store_rights_order`.`uid`,`offline_store_rights_order`.`code`,`offline_store_rights_order`.`offline_store_id`,`offline_store_rights_order`.`rights_id`,`offline_store_rights_order`.`rights_description`,`offline_store_rights_order`.`rights_content`,`offline_store_rights_order`.`createtime`,`offline_store_rights_order`.`usetime`,store.id AS store__id,store.uid AS store__uid,store.store_name AS store__store_name,store.logoimage AS store__logoimage,store.address AS store__address,store.lon AS store__lon,store.lat AS store__lat,store.onswitch AS store__onswitch,store.createtime AS store__createtime,store.updatetime AS store__updatetime,store.deletetime AS store__deletetime FROM `pl_shopro_offline_store_rights_order` `offline_store_rights_order` LEFT JOIN `pl_shopro_offline_store` `store` ON `offline_store_rights_order`.`offline_store_id`=`store`.`id` WHERE `offline_store_rights_order`.`uid` = 83 ORDER BY `offline_store_rights_order`.`id` DESC LIMIT 15,15 [ RunTime:0.073857s ] |
[ sql ] [ DB ] CONNECT:[ UseTime:0.110203s ] mysql:host=116.62.184.212;dbname=shop_wanhedashuj;charset=utf8mb4
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_user_token` [ RunTime:0.073344s ]
[ sql ] [ SQL ] SELECT * FROM `pl_user_token` WHERE `token` = 'c1dcced465b5170236f038309ba79fd718b38d2d' LIMIT 1 [ RunTime:0.072282s ]
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_user` [ RunTime:0.073363s ]
[ sql ] [ SQL ] SELECT * FROM `pl_user` WHERE `id` = 83 LIMIT 1 [ RunTime:0.072761s ]
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_shopro_offline_store_rights_order` [ RunTime:0.072322s ]
[ sql ] [ SQL ] SHOW COLUMNS FROM `pl_shopro_offline_store` [ RunTime:0.072191s ]
[ sql ] [ SQL ] SELECT COUNT(*) AS tp_count FROM `pl_shopro_offline_store_rights_order` `offline_store_rights_order` LEFT JOIN `pl_shopro_offline_store` `store` ON `offline_store_rights_order`.`offline_store_id`=`store`.`id` WHERE `offline_store_rights_order`.`uid` = 83 LIMIT 1 [ RunTime:0.071331s ]
[ sql ] [ SQL ] SELECT `offline_store_rights_order`.`id`,`offline_store_rights_order`.`uid`,`offline_store_rights_order`.`code`,`offline_store_rights_order`.`offline_store_id`,`offline_store_rights_order`.`rights_id`,`offline_store_rights_order`.`rights_description`,`offline_store_rights_order`.`rights_content`,`offline_store_rights_order`.`createtime`,`offline_store_rights_order`.`usetime`,store.id AS store__id,store.uid AS store__uid,store.store_name AS store__store_name,store.logoimage AS store__logoimage,store.address AS store__address,store.lon AS store__lon,store.lat AS store__lat,store.onswitch AS store__onswitch,store.createtime AS store__createtime,store.updatetime AS store__updatetime,store.deletetime AS store__deletetime FROM `pl_shopro_offline_store_rights_order` `offline_store_rights_order` LEFT JOIN `pl_shopro_offline_store` `store` ON `offline_store_rights_order`.`offline_store_id`=`store`.`id` WHERE `offline_store_rights_order`.`uid` = 83 ORDER BY `offline_store_rights_order`.`id` DESC LIMIT 15,15 [ RunTime:0.073857s ]
当前表为cart,如果购物车的记录不存在以下条件,在购物车的商品联查商品的数据(商品id=商品表的id),则删除购物车的记录
$cartData = self::whereNotExists(function ($query) {
$goodsTableName = (new Goods())->getQuery()->getTable();
$tableName = (new self())->getQuery()->getTable();
$query = $query->table($goodsTableName)->where($goodsTableName . '.id=' . $tableName . '.goods_id');
return $query;
})->where([
'user_id' => $user->id
])->delete(); |
$cartData = self::whereNotExists(function ($query) {
$goodsTableName = (new Goods())->getQuery()->getTable();
$tableName = (new self())->getQuery()->getTable();
$query = $query->table($goodsTableName)->where($goodsTableName . '.id=' . $tableName . '.goods_id');
return $query;
})->where([
'user_id' => $user->id
])->delete();
移除软删除属性操作 removeOption() 方法可以清理掉上查询保留的值 去除查询参数
$cartData = self::with(['goods' => function ($query) {
$query->removeOption('soft_delete');
}, 'sku_price' => function ($query) {
$query->removeOption('soft_delete');
}
])->where([
'user_id' => $user->id
]); |
$cartData = self::with(['goods' => function ($query) {
$query->removeOption('soft_delete');
}, 'sku_price' => function ($query) {
$query->removeOption('soft_delete');
}
])->where([
'user_id' => $user->id
]);
// 关闭 sql mode 的 ONLY_FULL_GROUP_BY
// 关闭 sql mode 的 ONLY_FULL_GROUP_BY
$oldModes = closeStrict(['ONLY_FULL_GROUP_BY']);
$cartData = $cartData->select();
// dump($user);
// dump($cartData);
// 恢复 sql mode
recoverStrict($oldModes);
* 删除 sql mode 指定模式,或者直接关闭 sql mode
*/
if (!function_exists('closeStrict')) {
function closeStrict($modes = [])
{
$modes = array_filter(is_array($modes) ? $modes : [$modes]);
$result = \think\Db::query("SELECT @@session.sql_mode");
$newModes = $oldModes = explode(',', ($result[0]['@@session.sql_mode'] ?? ''));
if ($modes) {
foreach ($modes as $mode) {
$delkey = array_search($mode, $newModes);
if ($delkey !== false) {
unset($newModes[$delkey]);
}
}
$newModes = join(',', array_values(array_filter($newModes)));
} else {
$newModes = '';
}
\think\Db::execute("set session sql_mode='" . $newModes . "'");
return $oldModes;
}
}
/**
* 重新打开被关闭的 sql mode
*/
if (!function_exists('recoverStrict')) {
function recoverStrict($modes = [], $append = false)
{
if ($append) {
$result = \think\Db::query("SELECT @@session.sql_mode");
$oldModes = explode(',', ($result[0]['@@session.sql_mode'] ?? ''));
$modes = array_values(array_filter(array_unique(array_merge($oldModes, $modes))));
}
\think\Db::execute("set session sql_mode='" . join(',', $modes) . "'");
}
} |
// 关闭 sql mode 的 ONLY_FULL_GROUP_BY
$oldModes = closeStrict(['ONLY_FULL_GROUP_BY']);
$cartData = $cartData->select();
// dump($user);
// dump($cartData);
// 恢复 sql mode
recoverStrict($oldModes);
* 删除 sql mode 指定模式,或者直接关闭 sql mode
*/
if (!function_exists('closeStrict')) {
function closeStrict($modes = [])
{
$modes = array_filter(is_array($modes) ? $modes : [$modes]);
$result = \think\Db::query("SELECT @@session.sql_mode");
$newModes = $oldModes = explode(',', ($result[0]['@@session.sql_mode'] ?? ''));
if ($modes) {
foreach ($modes as $mode) {
$delkey = array_search($mode, $newModes);
if ($delkey !== false) {
unset($newModes[$delkey]);
}
}
$newModes = join(',', array_values(array_filter($newModes)));
} else {
$newModes = '';
}
\think\Db::execute("set session sql_mode='" . $newModes . "'");
return $oldModes;
}
}
/**
* 重新打开被关闭的 sql mode
*/
if (!function_exists('recoverStrict')) {
function recoverStrict($modes = [], $append = false)
{
if ($append) {
$result = \think\Db::query("SELECT @@session.sql_mode");
$oldModes = explode(',', ($result[0]['@@session.sql_mode'] ?? ''));
$modes = array_values(array_filter(array_unique(array_merge($oldModes, $modes))));
}
\think\Db::execute("set session sql_mode='" . join(',', $modes) . "'");
}
}
self::where(['id' => $id, 'pid' => 0, 'status' => 'normal'])->with('children.children.children')->find();
public function children ()
{
return $this->hasMany(\addons\shopro\model\Category::class, 'pid', 'id')->where('status', 'normal')->order('weigh desc, id asc');
} |
self::where(['id' => $id, 'pid' => 0, 'status' => 'normal'])->with('children.children.children')->find();
public function children ()
{
return $this->hasMany(\addons\shopro\model\Category::class, 'pid', 'id')->where('status', 'normal')->order('weigh desc, id asc');
}