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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
736d3068
Commit
736d3068
authored
Jan 15, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored the active property of Connection and Transaction.
refactored Transaction.
parent
e0cd52fb
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
49 deletions
+58
-49
Connection.php
framework/db/Connection.php
+15
-23
Transaction.php
framework/db/Transaction.php
+38
-21
MysqlTestCase.php
tests/unit/MysqlTestCase.php
+1
-1
ConnectionTest.php
tests/unit/framework/db/ConnectionTest.php
+4
-4
No files found.
framework/db/Connection.php
View file @
736d3068
...
...
@@ -19,7 +19,7 @@ use yii\db\Exception;
* of the [[PDO PHP extension]](http://www.php.net/manual/en/ref.pdo.php).
*
* To establish a DB connection, set [[dsn]], [[username]] and [[password]], and then
* call [[open
]] or set [[active
]] to be true.
* call [[open
()
]] to be true.
*
* The following example shows how to create a Connection instance and establish
* the DB connection:
...
...
@@ -30,7 +30,7 @@ use yii\db\Exception;
* 'username' => $username,
* 'password' => $password,
* ));
* $connection->
active = true; // same as: $connection->
open();
* $connection->open();
* ~~~
*
* After the DB connection is established, one can execute SQL statements like the following:
...
...
@@ -60,12 +60,12 @@ use yii\db\Exception;
* ~~~
* $transaction = $connection->beginTransaction();
* try {
*
$connection->createCommand($sql1)->execute();
*
$connection->createCommand($sql2)->execute();
*
// ... executing other SQL statements ...
*
$transaction->commit();
*
$connection->createCommand($sql1)->execute();
*
$connection->createCommand($sql2)->execute();
*
// ... executing other SQL statements ...
*
$transaction->commit();
* } catch(Exception $e) {
*
$transaction->rollBack();
*
$transaction->rollBack();
* }
* ~~~
*
...
...
@@ -86,7 +86,7 @@ use yii\db\Exception;
* )
* ~~~
*
* @property boolean $
active Whether the DB connection is established
.
* @property boolean $
isActive Whether the DB connection is established. This property is read-only
.
* @property Transaction $currentTransaction The currently active transaction. Null if no active transaction.
* @property Driver $driver The database driver for the current connection.
* @property QueryBuilder $queryBuilder The query builder.
...
...
@@ -290,22 +290,12 @@ class Connection extends \yii\base\ApplicationComponent
* Returns a value indicating whether the DB connection is established.
* @return boolean whether the DB connection is established
*/
public
function
getActive
()
public
function
get
Is
Active
()
{
return
$this
->
pdo
!==
null
;
}
/**
* Opens or closes the DB connection.
* @param boolean $value whether to open or close the DB connection
* @throws Exception if there is any error when establishing the connection
*/
public
function
setActive
(
$value
)
{
$value
?
$this
->
open
()
:
$this
->
close
();
}
/**
* Turns on query caching.
* This method is provided as a shortcut to setting two properties that are related
* with query caching: [[queryCacheDuration]] and [[queryCacheDependency]].
...
...
@@ -433,7 +423,7 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
function
getCurrentTransaction
()
{
if
(
$this
->
_transaction
!==
null
&&
$this
->
_transaction
->
a
ctive
)
{
if
(
$this
->
_transaction
!==
null
&&
$this
->
_transaction
->
isA
ctive
)
{
return
$this
->
_transaction
;
}
else
{
return
null
;
...
...
@@ -446,10 +436,12 @@ class Connection extends \yii\base\ApplicationComponent
*/
public
function
beginTransaction
()
{
\Yii
::
trace
(
'Starting transaction'
,
__CLASS__
);
$this
->
open
();
$this
->
pdo
->
beginTransaction
();
return
$this
->
_transaction
=
new
Transaction
(
$this
);
$this
->
_transaction
=
new
Transaction
(
array
(
'connection'
=>
$this
,
));
$this
->
_transaction
->
begin
();
return
$this
->
_transaction
;
}
/**
...
...
framework/db/Transaction.php
View file @
736d3068
...
...
@@ -10,6 +10,7 @@
namespace
yii\db
;
use
yii\db\Exception
;
use
yii\base\BadConfigException
;
/**
* Transaction represents a DB transaction.
...
...
@@ -22,41 +23,57 @@ use yii\db\Exception;
* ~~~
* $transaction = $connection->beginTransaction();
* try {
*
$connection->createCommand($sql1)->execute();
*
$connection->createCommand($sql2)->execute();
*
//.... other SQL executions
*
$transaction->commit();
*
$connection->createCommand($sql1)->execute();
*
$connection->createCommand($sql2)->execute();
*
//.... other SQL executions
*
$transaction->commit();
* } catch(Exception $e) {
*
$transaction->rollBack();
*
$transaction->rollBack();
* }
* ~~~
*
* @property boolean $isActive Whether the transaction is active. This property is read-only.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Transaction
extends
\yii\base\Object
{
/**
* @var Connection the database connection that this transaction is associated with.
*/
public
$connection
;
/**
* @var boolean whether this transaction is active. Only an active transaction
* can [[commit()]] or [[rollBack()]]. This property is set true when the transaction is started.
*/
public
$active
;
private
$_active
=
false
;
/**
* @var Connection the database connection that this transaction is associated with.
* Returns a value indicating whether this transaction is active.
* @return boolean whether this transaction is active. Only an active transaction
* can [[commit()]] or [[rollBack()]].
*/
public
$connection
;
public
function
getIsActive
()
{
return
$this
->
_active
;
}
/**
* Constructor.
* @param Connection $connection the connection associated with this transaction
* @param array $config name-value pairs that will be used to initialize the object properties
* @see Connection::beginTransaction
* Begins a transaction.
* @throws BadConfigException if [[connection]] is null
*/
public
function
__construct
(
$connection
,
$config
=
array
()
)
public
function
begin
(
)
{
$this
->
active
=
true
;
$this
->
connection
=
$connection
;
parent
::
__construct
(
$config
);
if
(
!
$this
->
_active
)
{
if
(
$this
->
connection
===
null
)
{
throw
new
BadConfigException
(
'Transaction::connection must be set.'
);
}
\Yii
::
trace
(
'Starting transaction'
,
__CLASS__
);
$this
->
connection
->
open
();
$this
->
connection
->
pdo
->
beginTransaction
();
$this
->
_active
=
true
;
}
}
/**
...
...
@@ -65,10 +82,10 @@ class Transaction extends \yii\base\Object
*/
public
function
commit
()
{
if
(
$this
->
active
&&
$this
->
connection
->
getActive
()
)
{
if
(
$this
->
_active
&&
$this
->
connection
&&
$this
->
connection
->
isActive
)
{
\Yii
::
trace
(
'Committing transaction'
,
__CLASS__
);
$this
->
connection
->
pdo
->
commit
();
$this
->
active
=
false
;
$this
->
_
active
=
false
;
}
else
{
throw
new
Exception
(
'Failed to commit transaction: transaction was inactive.'
);
}
...
...
@@ -80,10 +97,10 @@ class Transaction extends \yii\base\Object
*/
public
function
rollback
()
{
if
(
$this
->
active
&&
$this
->
connection
->
getActive
()
)
{
if
(
$this
->
_active
&&
$this
->
connection
&&
$this
->
connection
->
isActive
)
{
\Yii
::
trace
(
'Rolling back transaction'
,
__CLASS__
);
$this
->
connection
->
pdo
->
rollBack
();
$this
->
active
=
false
;
$this
->
connection
->
pdo
->
commit
();
$this
->
_
active
=
false
;
}
else
{
throw
new
Exception
(
'Failed to roll back transaction: transaction was inactive.'
);
}
...
...
tests/unit/MysqlTestCase.php
View file @
736d3068
...
...
@@ -23,7 +23,7 @@ class MysqlTestCase extends TestCase
$db
->
username
=
$params
[
'username'
];
$db
->
password
=
$params
[
'password'
];
if
(
$reset
)
{
$db
->
active
=
true
;
$db
->
open
()
;
$lines
=
explode
(
';'
,
file_get_contents
(
$params
[
'fixture'
]));
foreach
(
$lines
as
$line
)
{
if
(
trim
(
$line
)
!==
''
)
{
...
...
tests/unit/framework/db/ConnectionTest.php
View file @
736d3068
...
...
@@ -20,15 +20,15 @@ class ConnectionTest extends \yiiunit\MysqlTestCase
{
$connection
=
$this
->
getConnection
(
false
);
$this
->
assertFalse
(
$connection
->
a
ctive
);
$this
->
assertFalse
(
$connection
->
isA
ctive
);
$this
->
assertEquals
(
null
,
$connection
->
pdo
);
$connection
->
open
();
$this
->
assertTrue
(
$connection
->
a
ctive
);
$this
->
assertTrue
(
$connection
->
isA
ctive
);
$this
->
assertTrue
(
$connection
->
pdo
instanceof
\PDO
);
$connection
->
close
();
$this
->
assertFalse
(
$connection
->
a
ctive
);
$this
->
assertFalse
(
$connection
->
isA
ctive
);
$this
->
assertEquals
(
null
,
$connection
->
pdo
);
$connection
=
new
Connection
;
...
...
@@ -41,7 +41,7 @@ class ConnectionTest extends \yiiunit\MysqlTestCase
{
$connection
=
$this
->
getConnection
(
false
);
$this
->
assertEquals
(
'mysql'
,
$connection
->
driverName
);
$this
->
assertFalse
(
$connection
->
a
ctive
);
$this
->
assertFalse
(
$connection
->
isA
ctive
);
}
function
testQuoteValue
()
...
...
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