Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
services
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tencent
services
Commits
6621d51c
Commit
6621d51c
authored
Jul 16, 2018
by
zhufx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
实体加注解标记分表
parent
483776c9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
69 deletions
+77
-69
src/Common/Entity/CommonEntity.php
+77
-2
src/Common/Entity/CommonSplitEntity.php
+0
-67
No files found.
src/Common/Entity/CommonEntity.php
View file @
6621d51c
...
@@ -2,14 +2,88 @@
...
@@ -2,14 +2,88 @@
/**
/**
* Created by PhpStorm.
* Created by PhpStorm.
* User: Administrator
* User: Administrator
* Date: 2018/7/
13
* Date: 2018/7/
2
* Time: 1
6:27
* Time: 1
3:49
*/
*/
namespace
Hdll\Services\Common\Entity
;
namespace
Hdll\Services\Common\Entity
;
use
Swoft\App
;
use
Swoft\Db\Bean\Collector\EntityCollector
;
use
Swoft\Db\Model
;
use
Swoft\Db\Model
;
use
Swoft\Exception\Exception
;
class
CommonEntity
extends
Model
class
CommonEntity
extends
Model
{
{
private
static
$commentString
;
public
function
__construct
(
array
$attributes
=
[])
{
parent
::
__construct
(
$attributes
);
//获取注解里的table
$res
=
new
\ReflectionClass
(
static
::
class
);
self
::
$commentString
=
$res
->
getDocComment
();
if
(
false
!==
strpos
(
self
::
$commentString
,
"
{
split
}
"
)
)
{
$dbNum
=
self
::
getDbNum
();
self
::
setTableName
(
$dbNum
);
}
$this
->
init
();
}
public
function
init
()
{
}
public
static
function
getDb
()
{
//获取注解里的table
$res
=
new
\ReflectionClass
(
static
::
class
);
self
::
$commentString
=
$res
->
getDocComment
();
if
(
false
!==
strpos
(
self
::
$commentString
,
"
{
split
}
"
)
)
{
$dbNum
=
self
::
getDbNum
();
self
::
setTableName
(
$dbNum
);
}
return
new
static
();
}
/**
* 获取数据库id
* @return int
* @throws Exception
*/
private
static
function
getDbNum
()
{
$user
=
App
::
getBean
(
User
::
class
);
$store
=
$user
->
getStoreId
();
if
(
empty
(
$store
)
)
{
throw
new
Exception
(
"店铺id获取失败"
);
}
return
$store
%
100
;
}
private
static
function
setTableName
(
$dbNum
)
{
preg_match
(
'/(?<=Table\(name=").*(?="\))/'
,
self
::
$commentString
,
$matches
);
$tableName
=
isset
(
$matches
[
0
])
?
$matches
[
0
]
:
''
;
EntityCollector
::
collect
(
static
::
class
,
new
\Swoft\Db\Bean\Annotation\Table
([
'name'
=>
$tableName
.
'_'
.
$dbNum
])
);
}
}
}
\ No newline at end of file
src/Common/Entity/CommonSplitEntity.php
deleted
100644 → 0
View file @
483776c9
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/7/2
* Time: 13:49
*/
namespace
Hdll\Services\Common\Entity
;
use
Swoft\App
;
use
Swoft\Db\Bean\Collector\EntityCollector
;
use
Swoft\Exception\Exception
;
class
CommonSplitEntity
extends
CommonEntity
{
public
function
__construct
(
array
$attributes
=
[])
{
parent
::
__construct
(
$attributes
);
$dbNum
=
self
::
getDbNum
();
self
::
setTableName
(
$dbNum
);
}
public
static
function
getDb
()
{
$dbNum
=
self
::
getDbNum
();
self
::
setTableName
(
$dbNum
);
return
new
static
();
}
/**
* 获取数据库id
* @return int
* @throws Exception
*/
private
static
function
getDbNum
()
{
$user
=
App
::
getBean
(
User
::
class
);
$store
=
$user
->
getStoreId
();
if
(
empty
(
$store
)
)
{
throw
new
Exception
(
"店铺id获取失败"
);
}
return
$store
%
100
;
}
private
static
function
setTableName
(
$dbNum
)
{
//获取注解里的table
$res
=
new
\ReflectionClass
(
static
::
class
);
$commentString
=
$res
->
getDocComment
();
preg_match
(
'/(?<=Table\(name=").*(?="\))/'
,
$commentString
,
$matches
);
$tableName
=
isset
(
$matches
[
0
])
?
$matches
[
0
]
:
''
;
EntityCollector
::
collect
(
static
::
class
,
new
\Swoft\Db\Bean\Annotation\Table
([
'name'
=>
$tableName
.
'_'
.
$dbNum
])
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment