Commit 3da9c61e by 王召彬
parents 28669e4a 9e7121ac
...@@ -8,9 +8,11 @@ ...@@ -8,9 +8,11 @@
namespace Hdll\Services\Common\Entity; namespace Hdll\Services\Common\Entity;
use Swoft\App; use Swoft\App;
use Swoft\Core\RequestContext;
use Swoft\Db\Bean\Collector\EntityCollector; use Swoft\Db\Bean\Collector\EntityCollector;
use Swoft\Db\Model; use Swoft\Db\Model;
use Swoft\Exception\Exception; use Swoft\Exception\Exception;
use Swoole\Http\Request;
class CommonEntity extends Model class CommonEntity extends Model
{ {
...@@ -23,20 +25,16 @@ class CommonEntity extends Model ...@@ -23,20 +25,16 @@ class CommonEntity extends Model
//获取注解里的table //获取注解里的table
$res = new \ReflectionClass(static::class); $res = new \ReflectionClass(static::class);
self::$commentString = $res->getDocComment(); self::$commentString = $res->getDocComment();
//分表逻辑
if ( false !== strpos(self::$commentString,"{split}") ) { $this->split();
$dbNum = self::getDbNum();
self::setTableName($dbNum);
}
$this->init(); $this->init();
} }
/**
* 初始化
*/
public function init() public function init()
{ {
...@@ -48,23 +46,68 @@ class CommonEntity extends Model ...@@ -48,23 +46,68 @@ class CommonEntity extends Model
$res = new \ReflectionClass(static::class); $res = new \ReflectionClass(static::class);
self::$commentString = $res->getDocComment(); self::$commentString = $res->getDocComment();
if ( false !== strpos(self::$commentString,"{split}") ) { //分表逻辑
$dbNum = self::getDbNum(); self::split();
self::setTableName($dbNum);
return new static;
}
/**
* 设置分表表名
* @throws Exception
*/
protected function split()
{
$request = RequestContext::getRequest();
if ( empty($request) ) {//初始化不做处理
return;
} }
return new static(); $testFlag = $request->query('flag');
if ( false === strpos(self::$commentString,"{split}") || $testFlag == 'hdllTest' ) {
return;
}
$dbNum = self::getDbNum();
$tableName = self::getTableName();
self::setTableName($tableName.'_'.$dbNum);
} }
/** /**
* 获取分库表明 * 获取分库表名
*
* @return string|void
* @throws Exception * @throws Exception
* @throws \ReflectionException
*/ */
public static function tableName() public static function tableName()
{ {
$num = self::getDbNum(); $request = RequestContext::getRequest();
return self::getTableName().'_'.$num; if ( empty($request) ) {//初始化不做处理
return;
}
$testFlag = $request->query('flag');
//获取注解里的table
$res = new \ReflectionClass(static::class);
self::$commentString = $res->getDocComment();
if ( false !== strpos(self::$commentString,"{split}") && $testFlag != 'hdllTest') {
$dbNum = self::getDbNum();
$tableName = self::getTableName().'_'.$dbNum;
self::setTableName($tableName);
return $tableName;
} else {
return self::getTableName();
}
} }
/** /**
...@@ -89,13 +132,11 @@ class CommonEntity extends Model ...@@ -89,13 +132,11 @@ class CommonEntity extends Model
* 设置表名 * 设置表名
* @param $dbNum * @param $dbNum
*/ */
private static function setTableName($dbNum) protected static function setTableName($tableName)
{ {
$tableName = self::getTableName();
EntityCollector::collect( EntityCollector::collect(
static::class, static::class,
new \Swoft\Db\Bean\Annotation\Table(['name' => $tableName.'_'.$dbNum]) new \Swoft\Db\Bean\Annotation\Table(['name' => $tableName])
); );
} }
...@@ -104,7 +145,7 @@ class CommonEntity extends Model ...@@ -104,7 +145,7 @@ class CommonEntity extends Model
* 获取原始表名 * 获取原始表名
* @return string * @return string
*/ */
private static function getTableName() protected static function getTableName()
{ {
preg_match('/(?<=Table\(name=").*(?="\))/', self::$commentString, $matches); preg_match('/(?<=Table\(name=").*(?="\))/', self::$commentString, $matches);
return $tableName = isset($matches[0])?$matches[0]:''; return $tableName = isset($matches[0])?$matches[0]:'';
......
...@@ -43,12 +43,6 @@ class OrderError ...@@ -43,12 +43,6 @@ class OrderError
const DB_ERROR = ["msg" => "数据库操作错误", "code" => 500, "errorCode" => 22118]; const DB_ERROR = ["msg" => "数据库操作错误", "code" => 500, "errorCode" => 22118];
public static function atranslate($message,$param) public static function atranslate($message,$param)
{ {
if ( empty($param) ) { if ( empty($param) ) {
......
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