执行生成的 SQL ,在本地 Windows 10 中可以,在 Liunx 容器中,未查询到记录(与 DIRECTORY_SEPARATOR 有关)

1、执行生成的 SQL ,在本地 Windows 10 中可以,在 Liunx 容器中,未查询到记录。在 Windows 10 下打印记录。如图1

图1

$rawSchema = ThemeAssetEntity::select('schema')
 ->where('theme_id', '=', $this->getThemeId())
 ->where('category', '=', ThemeAssetEntity::CATEGORY_PAGE)
 ->where('asset_key', 'regexp', $this->themeLayout->getTemplateExtensionsPattern($basename))
 ->first();
var_dump($rawSchema);
exit;

2、在 Liunx 容器 下打印记录。其结果为 NULL。如图2

图2

3、决定打印生成的 SQL,需要删除 ->first() 。进行一下对比。Windows 10 与 Linux 的分别如下,然后在 MySQL 中执行 Linux 下的 SQL ,查询结果为 NULL。区别在于 pages/\ 与 pages//。如图3

图3

$rawSchema = ThemeAssetEntity::select('schema')
 ->where('theme_id', '=', $this->getThemeId())
 ->where('category', '=', ThemeAssetEntity::CATEGORY_PAGE)
 ->where('asset_key', 'regexp', $this->themeLayout->getTemplateExtensionsPattern($basename));
$query = str_replace(array('?'), array('\'%s\''), $rawSchema->toSql());
$query = vsprintf($query, $rawSchema->getBindings());
dump($query);
exit;
select `schema` from `theme_asset` where `theme_id` = '96b1c8b9-5b18-4760-9e26-50ab009ac011' and `category` = 'page' and `asset_key` regexp '^pages/\product_detail(\.blade\.php|\.liquid|\.json)'
select `schema` from `theme_asset` where `theme_id` = '96dcab33-3e1d-4279-ab96-39693f03515f' and `category` = 'page' and `asset_key` regexp '^pages//product_detail(\.blade\.php|\.liquid|\.json)'

4、分析生成正则表达式的函数。打印 DIRECTORY_SEPARATOR,两个环境的结果是不一致的。分别为 \ 与 /

    public function getTemplateExtensionsPattern($basename)
    {
        $escapeDot = array_map(function($extension){
            return str_replace('.', '\\.' ,$extension);
        }, $this->getTemplateExtensions());
        print_r(DIRECTORY_SEPARATOR);
        exit;
       return sprintf('^%s%s%s%s', $this->getPageDir(), '/'.DIRECTORY_SEPARATOR, $basename, '('.join('|', $escapeDot) .')');
    }
\
/

5、删除掉 .DIRECTORY_SEPARATOR,因为其在两个操作系统中不一致。且因为在表中仅使用 /,无需要再转义。最终生成的 SQL 在两个操作系统中一致。且能够查询到结果。如图4

图4

public function getTemplateExtensionsPattern($basename)
{
 $escapeDot = array_map(function($extension){
  return str_replace('.', '\\.' ,$extension);
 }, $this->getTemplateExtensions());

   return sprintf('^%s%s%s%s', $this->getPageDir(), '/', $basename, '('.join('|', $escapeDot) .')');
}
"select `schema` from `theme_asset` where `theme_id` = '96dcab33-3e1d-4279-ab96-39693f03515f' and `category` = 'page' and `asset_key` regexp '^pages/product_detail(\.blade\.php|\.liquid|\.json)'"
永夜