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
38df4801
Commit
38df4801
authored
Feb 09, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2381 from Ragazzo/fixture_controller_imporved
improved fixture command
parents
a6e45c43
325c7a18
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
64 deletions
+10
-64
FixtureController.php
framework/console/controllers/FixtureController.php
+10
-64
No files found.
framework/console/controllers/FixtureController.php
View file @
38df4801
...
@@ -16,32 +16,15 @@ use yii\test\FixtureTrait;
...
@@ -16,32 +16,15 @@ use yii\test\FixtureTrait;
/**
/**
* This command manages loading and unloading fixtures.
* This command manages loading and unloading fixtures.
* You can specify different options of this command to point fixture manager
* to the specific tables of the different database connections.
*
* To use this command simply configure your console.php config like this:
*
* ~~~
* 'db' => [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'mysql:host=localhost;dbname={your_database}',
* 'username' => '{your_db_user}',
* 'password' => '',
* 'charset' => 'utf8',
* ],
* ~~~
*
*
* ~~~
* ~~~
* #load fixtures
under $fixturePath
from UsersFixture class with default namespace "tests\unit\fixtures"
* #load fixtures from UsersFixture class with default namespace "tests\unit\fixtures"
* yii fixture/load User
* yii fixture/load User
*
*
* #also a short version of this command (generate action is default)
* #also a short version of this command (generate action is default)
* yii fixture User
* yii fixture User
*
*
* #load fixtures under $fixturePath with the different database connection
* #load fixtures with different namespace.
* yii fixture/load User --db=someOtherDbConnection
*
* #load fixtures under different $fixturePath.
* yii fixture/load User --namespace=alias\my\custom\namespace\goes\here
* yii fixture/load User --namespace=alias\my\custom\namespace\goes\here
* ~~~
* ~~~
*
*
...
@@ -61,11 +44,7 @@ class FixtureController extends Controller
...
@@ -61,11 +44,7 @@ class FixtureController extends Controller
/**
/**
* @var string controller default action ID.
* @var string controller default action ID.
*/
*/
public
$defaultAction
=
'apply'
;
public
$defaultAction
=
'load'
;
/**
* @var string id of the database connection component of the application.
*/
public
$db
=
'db'
;
/**
/**
* @var string default namespace to search fixtures in
* @var string default namespace to search fixtures in
*/
*/
...
@@ -85,7 +64,7 @@ class FixtureController extends Controller
...
@@ -85,7 +64,7 @@ class FixtureController extends Controller
public
function
globalOptions
()
public
function
globalOptions
()
{
{
return
array_merge
(
parent
::
globalOptions
(),
[
return
array_merge
(
parent
::
globalOptions
(),
[
'
db'
,
'
namespace'
,
'globalFixtures'
'namespace'
,
'globalFixtures'
]);
]);
}
}
...
@@ -126,23 +105,16 @@ class FixtureController extends Controller
...
@@ -126,23 +105,16 @@ class FixtureController extends Controller
throw
new
Exception
(
'No fixtures were found in namespace: "'
.
$this
->
namespace
.
'"'
.
''
);
throw
new
Exception
(
'No fixtures were found in namespace: "'
.
$this
->
namespace
.
'"'
.
''
);
}
}
$transaction
=
$this
->
getDbConnection
()
->
beginTransaction
();
$fixturesObjects
=
$this
->
createFixtures
(
$fixtures
);
$this
->
unloadFixtures
(
$fixturesObjects
);
try
{
$this
->
loadFixtures
(
$fixturesObjects
);
$this
->
loadFixtures
(
$this
->
createFixtures
(
$fixtures
));
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
$transaction
->
rollback
();
$this
->
stdout
(
"Exception occurred, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
throw
$e
;
}
$this
->
notifyLoaded
(
$fixtures
);
$this
->
notifyLoaded
(
$fixtures
);
}
}
/**
/**
* Unloads given fixtures. You can clear environment and unload multiple fixtures by specifying
* Unloads given fixtures. You can clear environment and unload multiple fixtures by specifying
* their names separated with commas, like: User,UserProfile,MyCustom. Be sure there is no
* their names separated with commas, like: User,UserProfile,MyCustom. Be sure there is no
* whitespace between
tables
names.
* whitespace between names.
* @param array|string $fixtures
* @param array|string $fixtures
* @param array|string $except
* @param array|string $except
*/
*/
...
@@ -169,43 +141,17 @@ class FixtureController extends Controller
...
@@ -169,43 +141,17 @@ class FixtureController extends Controller
}
}
$filtered
=
array_diff
(
$foundFixtures
,
$except
);
$filtered
=
array_diff
(
$foundFixtures
,
$except
);
$fixtures
=
$this
->
getFixturesConfig
(
array_merge
(
$this
->
globalFixtures
,
$filtered
));
$fixtures
=
$this
->
getFixturesConfig
(
array_merge
(
$this
->
globalFixtures
,
$filtered
));
if
(
!
$fixtures
)
{
if
(
!
$fixtures
)
{
throw
new
Exception
(
'No fixtures were found in namespace: '
.
$this
->
namespace
.
'".'
);
throw
new
Exception
(
'No fixtures were found in namespace: '
.
$this
->
namespace
.
'".'
);
}
}
$transaction
=
$this
->
getDbConnection
()
->
beginTransaction
();
$this
->
unloadFixtures
(
$this
->
createFixtures
(
$fixtures
));
try
{
$this
->
unloadFixtures
(
$this
->
createFixtures
(
$fixtures
));
$transaction
->
commit
();
}
catch
(
\Exception
$e
)
{
$transaction
->
rollback
();
$this
->
stdout
(
"Exception occurred, transaction rollback. Tables will be in same state.
\n
"
,
Console
::
BG_RED
);
throw
$e
;
}
$this
->
notifyUnloaded
(
$fixtures
);
$this
->
notifyUnloaded
(
$fixtures
);
}
}
/**
/**
* Returns database connection component
* @return \yii\db\Connection
* @throws \yii\console\Exception if [[db]] is invalid.
*/
public
function
getDbConnection
()
{
$db
=
Yii
::
$app
->
getComponent
(
$this
->
db
);
if
(
$db
===
null
)
{
throw
new
Exception
(
"There is no database connection component with id
\"
{
$this
->
db
}
\"
."
);
}
return
$db
;
}
/**
* Notifies user that fixtures were successfully loaded.
* Notifies user that fixtures were successfully loaded.
* @param array $fixtures
* @param array $fixtures
*/
*/
...
...
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