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) {
});
```
You may also directly set `$db->enableSlaves` to be false to direct all queries to the master connection.
Working with database schema
----------------------------
...
...
framework/db/Connection.php
View file @
65987f62
...
...
@@ -296,12 +296,12 @@ class Connection extends Component
* @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.
*/
public
$enableSlave
=
true
;
public
$enableSlave
s
=
true
;
/**
* @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.
* @see enableSlave
* @see enableSlave
s
* @see slaveConfig
*/
public
$slaves
=
[];
...
...
@@ -711,11 +711,12 @@ class Connection extends Component
}
/**
* Returns the PDO instance for
read queries
.
* When [[enableSlave]] 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.
* Returns the PDO instance for
the currently active slave connection
.
* 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.
* @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
)
{
...
...
@@ -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]].
* @return PDO the PDO instance for
write queries
.
* @return PDO the PDO instance for
the currently active master connection
.
*/
public
function
getMasterPdo
()
{
...
...
@@ -740,13 +741,14 @@ class Connection extends Component
/**
* 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.
* @param boolean $fallbackToMaster whether to return a master connection in case none of the slave connections is available.
* @return Connection the currently active slave. Null is returned if there is slave available.
* 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 there is no slave connection 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
)
{
if
(
!
$this
->
enableSlave
)
{
if
(
!
$this
->
enableSlave
s
)
{
return
$fallbackToMaster
?
$this
:
null
;
}
...
...
@@ -775,15 +777,16 @@ class Connection extends Component
*/
public
function
useMaster
(
callable
$callback
)
{
$enableSlave
=
$this
->
enableSlave
;
$this
->
enableSlave
=
false
;
$enableSlave
=
$this
->
enableSlave
s
;
$this
->
enableSlave
s
=
false
;
$result
=
call_user_func
(
$callback
,
$this
);
$this
->
enableSlave
=
$enableSlave
;
$this
->
enableSlave
s
=
$enableSlave
;
return
$result
;
}
/**
* 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 $sharedConfig the configuration common to those given in `$pool`.
* @return Connection the opened DB connection, or null if no server is available
...
...
@@ -824,6 +827,7 @@ class Connection extends Component
}
catch
(
\Exception
$e
)
{
Yii
::
warning
(
"Connection (
{
$config
[
'dsn'
]
}
) failed: "
.
$e
->
getMessage
(),
__METHOD__
);
if
(
$cache
instanceof
Cache
)
{
// mark this server as dead and only retry it after the specified interval
$cache
->
set
(
$key
,
1
,
$this
->
serverRetryInterval
);
}
}
...
...
tests/unit/framework/db/DatabaseTestCase.php
View file @
65987f62
...
...
@@ -44,26 +44,32 @@ abstract class DatabaseTestCase extends TestCase
if
(
!
$reset
&&
$this
->
db
)
{
return
$this
->
db
;
}
$db
=
new
\yii\db\Connection
;
$db
->
dsn
=
$this
->
database
[
'dsn'
];
if
(
isset
(
$this
->
database
[
'username'
]))
{
$db
->
username
=
$this
->
database
[
'username'
];
$db
->
password
=
$this
->
database
[
'password'
];
$config
=
$this
->
database
;
if
(
isset
(
$config
[
'fixture'
]))
{
$fixture
=
$config
[
'fixture'
];
unset
(
$config
[
'fixture'
]);
}
else
{
$fixture
=
null
;
}
if
(
isset
(
$this
->
database
[
'attributes'
]))
{
$db
->
attributes
=
$this
->
database
[
'attributes'
];
return
$this
->
db
=
$this
->
prepareDatabase
(
$config
,
$fixture
);
}
public
function
prepareDatabase
(
$config
,
$fixture
)
{
if
(
!
isset
(
$config
[
'class'
]))
{
$config
[
'class'
]
=
'yii\db\Connection'
;
}
if
(
$open
)
{
$db
->
open
();
$lines
=
explode
(
';'
,
file_get_contents
(
$this
->
database
[
'fixture'
]));
/* @var $db \yii\db\Connection */
$db
=
\Yii
::
createObject
(
$config
);
$db
->
open
();
if
(
$fixture
!==
null
)
{
$lines
=
explode
(
';'
,
file_get_contents
(
$fixture
));
foreach
(
$lines
as
$line
)
{
if
(
trim
(
$line
)
!==
''
)
{
$db
->
pdo
->
exec
(
$line
);
}
}
}
$this
->
db
=
$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