Commit 3efc775e by feixiang

query分表重构

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