Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
65987f62
Commit
65987f62
authored
Jul 12, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test WIP
parent
c64ffdb2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
28 deletions
+40
-28
db-dao.md
docs/guide/db-dao.md
+2
-0
Connection.php
framework/db/Connection.php
+20
-16
DatabaseTestCase.php
tests/unit/framework/db/DatabaseTestCase.php
+18
-12
No files found.
docs/guide/db-dao.md
View file @
65987f62
...
@@ -495,6 +495,8 @@ $rows = $db->useMaster(function ($db) {
...
@@ -495,6 +495,8 @@ $rows = $db->useMaster(function ($db) {
});
});
```
```
You may also directly set `$db->enableSlaves` to be false to direct all queries to the master connection.
Working with database schema
Working with database schema
----------------------------
----------------------------
...
...
framework/db/Connection.php
View file @
65987f62
...
@@ -296,12 +296,12 @@ class Connection extends Component
...
@@ -296,12 +296,12 @@ class Connection extends Component
* @var boolean whether to enable read/write splitting by using [[slaves]] to read data.
* @var boolean whether to enable read/write splitting by using [[slaves]] to read data.
* Note that if [[slaves]] is empty, read/write splitting will NOT be enabled no matter what value this property takes.
* Note that if [[slaves]] is empty, read/write splitting will NOT be enabled no matter what value this property takes.
*/
*/
public
$enableSlave
=
true
;
public
$enableSlave
s
=
true
;
/**
/**
* @var array list of slave connection configurations. Each configuration is used to create a slave DB connection.
* @var array list of slave connection configurations. Each configuration is used to create a slave DB connection.
* When [[enableSlave]] is true, one of these configurations will be chosen and used to create a DB connection
* When [[enableSlave
s
]] is true, one of these configurations will be chosen and used to create a DB connection
* for performing read queries only.
* for performing read queries only.
* @see enableSlave
* @see enableSlave
s
* @see slaveConfig
* @see slaveConfig
*/
*/
public
$slaves
=
[];
public
$slaves
=
[];
...
@@ -711,11 +711,12 @@ class Connection extends Component
...
@@ -711,11 +711,12 @@ class Connection extends Component
}
}
/**
/**
* Returns the PDO instance for
read queries
.
* Returns the PDO instance for
the currently active slave connection
.
* When [[enableSlave]] is true, one of the slaves will be used for read queries, and its PDO instance
* When [[enableSlave
s
]] is true, one of the slaves will be used for read queries, and its PDO instance
* will be returned by this method.
If no slave is available, the [[writePdo]] will be returned.
* will be returned by this method.
* @param boolean $fallbackToMaster whether to return a master PDO in case none of the slave connections is available.
* @param boolean $fallbackToMaster whether to return a master PDO in case none of the slave connections is available.
* @return PDO the PDO instance for read queries. Null is returned if no server is available.
* @return PDO the PDO instance for the currently active slave connection. Null is returned if no slave connection
* is available and `$fallbackToMaster` is false.
*/
*/
public
function
getSlavePdo
(
$fallbackToMaster
=
true
)
public
function
getSlavePdo
(
$fallbackToMaster
=
true
)
{
{
...
@@ -728,9 +729,9 @@ class Connection extends Component
...
@@ -728,9 +729,9 @@ class Connection extends Component
}
}
/**
/**
* Returns the PDO instance for
write queries
.
* Returns the PDO instance for
the currently active master connection
.
* This method will open the master DB connection and then return [[pdo]].
* This method will open the master DB connection and then return [[pdo]].
* @return PDO the PDO instance for
write queries
.
* @return PDO the PDO instance for
the currently active master connection
.
*/
*/
public
function
getMasterPdo
()
public
function
getMasterPdo
()
{
{
...
@@ -740,13 +741,14 @@ class Connection extends Component
...
@@ -740,13 +741,14 @@ class Connection extends Component
/**
/**
* Returns the currently active slave connection.
* Returns the currently active slave connection.
* If this method is called the first time, it will try to open a slave connection when [[enableSlave]] is true.
* If this method is called the first time, it will try to open a slave connection when [[enableSlaves]] is true.
* @param boolean $fallbackToMaster whether to return a master connection in case none of the slave connections is available.
* @param boolean $fallbackToMaster whether to return a master connection in case there is no slave connection available.
* @return Connection the currently active slave. Null is returned if there is slave available.
* @return Connection the currently active slave connection. Null is returned if there is slave available and
* `$fallbackToMaster` is false.
*/
*/
public
function
getSlave
(
$fallbackToMaster
=
true
)
public
function
getSlave
(
$fallbackToMaster
=
true
)
{
{
if
(
!
$this
->
enableSlave
)
{
if
(
!
$this
->
enableSlave
s
)
{
return
$fallbackToMaster
?
$this
:
null
;
return
$fallbackToMaster
?
$this
:
null
;
}
}
...
@@ -775,15 +777,16 @@ class Connection extends Component
...
@@ -775,15 +777,16 @@ class Connection extends Component
*/
*/
public
function
useMaster
(
callable
$callback
)
public
function
useMaster
(
callable
$callback
)
{
{
$enableSlave
=
$this
->
enableSlave
;
$enableSlave
=
$this
->
enableSlave
s
;
$this
->
enableSlave
=
false
;
$this
->
enableSlave
s
=
false
;
$result
=
call_user_func
(
$callback
,
$this
);
$result
=
call_user_func
(
$callback
,
$this
);
$this
->
enableSlave
=
$enableSlave
;
$this
->
enableSlave
s
=
$enableSlave
;
return
$result
;
return
$result
;
}
}
/**
/**
* Opens the connection to a server in the pool.
* Opens the connection to a server in the pool.
* This method implements the load balancing among the given list of the servers.
* @param array $pool the list of connection configurations in the server pool
* @param array $pool the list of connection configurations in the server pool
* @param array $sharedConfig the configuration common to those given in `$pool`.
* @param array $sharedConfig the configuration common to those given in `$pool`.
* @return Connection the opened DB connection, or null if no server is available
* @return Connection the opened DB connection, or null if no server is available
...
@@ -824,6 +827,7 @@ class Connection extends Component
...
@@ -824,6 +827,7 @@ class Connection extends Component
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
Yii
::
warning
(
"Connection (
{
$config
[
'dsn'
]
}
) failed: "
.
$e
->
getMessage
(),
__METHOD__
);
Yii
::
warning
(
"Connection (
{
$config
[
'dsn'
]
}
) failed: "
.
$e
->
getMessage
(),
__METHOD__
);
if
(
$cache
instanceof
Cache
)
{
if
(
$cache
instanceof
Cache
)
{
// mark this server as dead and only retry it after the specified interval
$cache
->
set
(
$key
,
1
,
$this
->
serverRetryInterval
);
$cache
->
set
(
$key
,
1
,
$this
->
serverRetryInterval
);
}
}
}
}
...
...
tests/unit/framework/db/DatabaseTestCase.php
View file @
65987f62
...
@@ -44,26 +44,32 @@ abstract class DatabaseTestCase extends TestCase
...
@@ -44,26 +44,32 @@ abstract class DatabaseTestCase extends TestCase
if
(
!
$reset
&&
$this
->
db
)
{
if
(
!
$reset
&&
$this
->
db
)
{
return
$this
->
db
;
return
$this
->
db
;
}
}
$db
=
new
\yii\db\Connection
;
$config
=
$this
->
database
;
$db
->
dsn
=
$this
->
database
[
'dsn'
];
if
(
isset
(
$config
[
'fixture'
]))
{
if
(
isset
(
$this
->
database
[
'username'
]))
{
$fixture
=
$config
[
'fixture'
];
$db
->
username
=
$this
->
database
[
'username'
];
unset
(
$config
[
'fixture'
]);
$db
->
password
=
$this
->
database
[
'password'
];
}
else
{
$fixture
=
null
;
}
}
if
(
isset
(
$this
->
database
[
'attributes'
]))
{
return
$this
->
db
=
$this
->
prepareDatabase
(
$config
,
$fixture
);
$db
->
attributes
=
$this
->
database
[
'attributes'
];
}
public
function
prepareDatabase
(
$config
,
$fixture
)
{
if
(
!
isset
(
$config
[
'class'
]))
{
$config
[
'class'
]
=
'yii\db\Connection'
;
}
}
if
(
$open
)
{
/* @var $db \yii\db\Connection */
$db
->
open
();
$db
=
\Yii
::
createObject
(
$config
);
$lines
=
explode
(
';'
,
file_get_contents
(
$this
->
database
[
'fixture'
]));
$db
->
open
();
if
(
$fixture
!==
null
)
{
$lines
=
explode
(
';'
,
file_get_contents
(
$fixture
));
foreach
(
$lines
as
$line
)
{
foreach
(
$lines
as
$line
)
{
if
(
trim
(
$line
)
!==
''
)
{
if
(
trim
(
$line
)
!==
''
)
{
$db
->
pdo
->
exec
(
$line
);
$db
->
pdo
->
exec
(
$line
);
}
}
}
}
}
}
$this
->
db
=
$db
;
return
$db
;
return
$db
;
}
}
}
}
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