Commit f1d74d8a by feixiang

单元测试 分表处理

parent c090b428
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Hdll\Services\Common\Entity; namespace Hdll\Services\Common\Entity;
use Hdll\Services\Common\Bean\Collector\SplitCollector; use Hdll\Services\Common\Bean\Collector\SplitCollector;
use Hdll\Services\Common\Enum\TestEnum;
use Hdll\Services\Common\Exception\CommonException; use Hdll\Services\Common\Exception\CommonException;
use Swoft\Core\RequestContext; use Swoft\Core\RequestContext;
use Swoft\Db\Bean\Collector\EntityCollector; use Swoft\Db\Bean\Collector\EntityCollector;
...@@ -23,32 +24,36 @@ class Split ...@@ -23,32 +24,36 @@ class Split
$map = array_flip($collector['map']); $map = array_flip($collector['map']);
} }
//如果是实体且为分表实体 if ( ! self::isTesting() ) {
if (strpos($tableName, '\\') !== false && isset($collector[$tableName])) { //如果是实体且为分表实体
$className = $tableName; if (strpos($tableName, '\\') !== false && isset($collector[$tableName])) {
$tableName = $collector[$tableName]['table']; $className = $tableName;
$tableName = $collector[$tableName]['table'];
$entityCollector = EntityCollector::getCollector(); $entityCollector = EntityCollector::getCollector();
//设置分表对应的className //设置分表对应的className
EntityCollector::setCollector($tableName.'_'.self::getDbNum($className), $tableName); EntityCollector::setCollector($tableName.'_'.self::getDbNum($className), $tableName);
//获取分表名 //获取分表名
$tableName = $tableName.'_'.self::getDbNum($className); $tableName = $tableName.'_'.self::getDbNum($className);
//设置分表类型 //设置分表类型
$query->selectInstance($entityCollector[$className]['instance']); $query->selectInstance($entityCollector[$className]['instance']);
} elseif ( isset($map[$tableName]) ) { //非实体且分表 } elseif ( isset($map[$tableName]) ) { //非实体且分表
$className = $map[$tableName]; $className = $map[$tableName];
$defaultTbleName = $tableName; $defaultTbleName = $tableName;
$tableName = $tableName.'_'.self::getDbNum($className); $tableName = $tableName.'_'.self::getDbNum($className);
//设置分表对应的className //设置分表对应的className
EntityCollector::setCollector($tableName, $defaultTbleName); EntityCollector::setCollector($tableName, $defaultTbleName);
}
} }
return $tableName; return $tableName;
} }
...@@ -63,4 +68,12 @@ class Split ...@@ -63,4 +68,12 @@ class Split
} }
return $storeId % $splitNum; return $storeId % $splitNum;
} }
private function isTesting()
{
$data = RequestContext::getContextData();
$storeId = $data['userInfo']['storeId'];
return intval($storeId) === TestEnum::TEST_STORE_ID;
}
} }
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/25
* Time: 13:22
*/
namespace Hdll\Services\Common\Enum;
class TestEnum
{
const TEST_STORE_ID = -3;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment