Commit 52e40b27 by feixiang

分表

parent a7f73166
......@@ -9,9 +9,7 @@
*/
namespace Swoft\Db;
use Hdll\Services\Common\Bean\Collector\SplitCollector;
use Swoft\Core\RequestContext;
use Swoft\Db\Bean\Collector\EntityCollector;
use Hdll\Services\Common\Entity\Split;
/**
* Query
......@@ -28,50 +26,12 @@ class Query
public static function table(string $tableName, string $alias = null): QueryBuilder
{
$query = new QueryBuilder();
$collector = SplitCollector::getCollector();//分表搜集器
if ( $collector['map'] !== null && is_array($collector['map'])) {
$map = array_flip($collector['map']);
}
//如果是实体且为分表实体
if (strpos($tableName, '\\') !== false && isset($collector[$tableName])) {
$className = $tableName;
$tableName = $collector[$tableName]['table'];
$entityCollector = EntityCollector::getCollector();
//设置分表对应的className
EntityCollector::setCollector($tableName.'_'.self::getDbNum($className), $tableName);
//获取分表名
$tableName = $tableName.'_'.self::getDbNum($className);
//设置分表类型
$query->selectInstance($entityCollector[$className]['instance']);
} elseif ( isset($map[$tableName]) ) { //非实体且分表
$className = $map[$tableName];
$defaultTbleName = $tableName;
$tableName = $tableName.'_'.self::getDbNum($className);
//设置分表对应的className
EntityCollector::setCollector($tableName, $defaultTbleName);
}
//初始化分表
Split::initSplit($tableName, $query);
$query = $query->table($tableName, $alias);
return $query;
}
private function getDbNum($className)
{
$splitCollector = SplitCollector::getCollector();
$splitNum = $splitCollector[$className]['num'];
$data = RequestContext::getContextData();
$storeId = $data['userInfo']['storeId'];
return $storeId % $splitNum;
}
}
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/18
* Time: 10:21
*/
namespace Hdll\Services\Common\Entity;
use Hdll\Services\Common\Bean\Collector\SplitCollector;
use Swoft\Core\RequestContext;
use Swoft\Db\Bean\Collector\EntityCollector;
use Swoft\Db\QueryBuilder;
class Split
{
public static function initSplit($tableName, QueryBuilder $query)
{
$collector = SplitCollector::getCollector();//分表搜集器
if ( $collector['map'] !== null && is_array($collector['map'])) {
$map = array_flip($collector['map']);
}
//如果是实体且为分表实体
if (strpos($tableName, '\\') !== false && isset($collector[$tableName])) {
$className = $tableName;
$tableName = $collector[$tableName]['table'];
$entityCollector = EntityCollector::getCollector();
//设置分表对应的className
EntityCollector::setCollector($tableName.'_'.self::getDbNum($className), $tableName);
//获取分表名
$tableName = $tableName.'_'.self::getDbNum($className);
//设置分表类型
$query->selectInstance($entityCollector[$className]['instance']);
} elseif ( isset($map[$tableName]) ) { //非实体且分表
$className = $map[$tableName];
$defaultTbleName = $tableName;
$tableName = $tableName.'_'.self::getDbNum($className);
//设置分表对应的className
EntityCollector::setCollector($tableName, $defaultTbleName);
}
return $tableName;
}
private function getDbNum($className)
{
$splitCollector = SplitCollector::getCollector();
$splitNum = $splitCollector[$className]['num'];
$data = RequestContext::getContextData();
$storeId = $data['userInfo']['storeId'];
return $storeId % $splitNum;
}
}
\ No newline at end of file
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