Commit 3efc775e by feixiang

query分表重构

parent d448c95a
......@@ -17,41 +17,23 @@ use Doctrine\Common\Annotations\Annotation\Target;
*/
class Split
{
private $name='';
private $table='';
private $num=100;
public function __construct(array $values)
{
if (isset($values['value'])) {
$this->name = $values['value'];
}
if (isset($values['name'])) {
$this->name = $values['name'];
}
if (isset($values['table'])) {
$this->table = $values['table'];
if (isset($values['num'])) {
$this->num = $values['num'];
}
}
public function setName($name)
public function setNum($num)
{
$this->name = $name;
$this->num = $num;
}
public function setTable($table)
{
$this->table = $table;
}
public function getName() : string
{
return $this->name;
}
public function getTable()
public function getNum()
{
return $this->table;
return $this->num;
}
}
\ No newline at end of file
......@@ -16,7 +16,7 @@ class SplitCollector implements CollectorInterface
/**
* @var array
*/
private static $auth = [];
private static $split = [];
public static function collect(string $className,
$objectAnnotation = null,
......@@ -28,15 +28,17 @@ class SplitCollector implements CollectorInterface
$entityCollector = EntityCollector::getCollector();
self::$auth[$className]['table'] = $entityCollector[$className]['table']['name'];;
self::$auth['map'][$className] = $entityCollector[$className]['table']['name'];
self::$split[$className]['table'] = $entityCollector[$className]['table']['name'];
self::$split[$className]['num'] = $objectAnnotation->getNum();
self::$split['map'][$className] = $entityCollector[$className]['table']['name'];
}
}
public static function getCollector()
{
return self::$auth;
return self::$split;
}
}
\ No newline at end of file
......@@ -39,17 +39,17 @@ class Query
$entityCollector = EntityCollector::getCollector();
//设置分表对应的className
EntityCollector::setCollector($tableName.'_'.self::getDbNum(), $tableName);
EntityCollector::setCollector($tableName.'_'.self::getDbNum($className), $tableName);
//获取分表名
$tableName = $tableName.'_'.self::getDbNum();
$tableName = $tableName.'_'.self::getDbNum($className);
//设置分表类型
$query->selectInstance($entityCollector[$className]['instance']);
} elseif ($map = array_flip($collector['map']) && isset($map[$tableName]) ) { //非实体
$tableName = $tableName.'_'.self::getDbNum();
$tableName = $tableName.'_'.self::getDbNum($map[$tableName]);
}
......@@ -57,10 +57,12 @@ class Query
return $query;
}
private function getDbNum()
private function getDbNum($className)
{
$splitCollector = SplitCollector::getCollector();
$splitNum = $splitCollector[$className]['num'];
$data = RequestContext::getContextData();
$storeId = $data['userInfo']['storeId'];
return $storeId % 100;
return $storeId % $splitNum;
}
}
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