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
3efc775e
Commit
3efc775e
authored
Sep 14, 2018
by
feixiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
query分表重构
parent
d448c95a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
34 deletions
+22
-34
src/Common/Bean/Annotation/Split.php
+8
-25
src/Common/Bean/Collector/SplitCollector.php
+7
-4
src/Common/Entity/Query.php
+7
-5
No files found.
src/Common/Bean/Annotation/Split.php
View file @
3efc775e
...
...
@@ -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
setN
ame
(
$name
)
public
function
setN
um
(
$num
)
{
$this
->
n
ame
=
$name
;
$this
->
n
um
=
$num
;
}
public
function
setTable
(
$table
)
{
$this
->
table
=
$table
;
}
public
function
getName
()
:
string
{
return
$this
->
name
;
}
public
function
get
Table
()
public
function
get
Num
()
{
return
$this
->
table
;
return
$this
->
num
;
}
}
\ No newline at end of file
src/Common/Bean/Collector/SplitCollector.php
View file @
3efc775e
...
...
@@ -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
src/Common/Entity/Query.php
View file @
3efc775e
...
...
@@ -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
;
}
}
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