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
325c7a18
Commit
325c7a18
authored
Feb 09, 2014
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved fixture command
parent
7950b793
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 @
325c7a18
...
...
@@ -16,32 +16,15 @@ use yii\test\FixtureTrait;
/**
* 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
*
* #also a short version of this command (generate action is default)
* yii fixture User
*
* #load fixtures under $fixturePath with the different database connection
* yii fixture/load User --db=someOtherDbConnection
*
* #load fixtures under different $fixturePath.
* #load fixtures with different namespace.
* yii fixture/load User --namespace=alias\my\custom\namespace\goes\here
* ~~~
*
...
...
@@ -61,11 +44,7 @@ class FixtureController extends Controller
/**
* @var string controller default action ID.
*/
public
$defaultAction
=
'apply'
;
/**
* @var string id of the database connection component of the application.
*/
public
$db
=
'db'
;
public
$defaultAction
=
'load'
;
/**
* @var string default namespace to search fixtures in
*/
...
...
@@ -85,7 +64,7 @@ class FixtureController extends Controller
public
function
globalOptions
()
{
return
array_merge
(
parent
::
globalOptions
(),
[
'
db'
,
'
namespace'
,
'globalFixtures'
'namespace'
,
'globalFixtures'
]);
}
...
...
@@ -126,23 +105,16 @@ class FixtureController extends Controller
throw
new
Exception
(
'No fixtures were found in namespace: "'
.
$this
->
namespace
.
'"'
.
''
);
}
$transaction
=
$this
->
getDbConnection
()
->
beginTransaction
();
try
{
$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
;
}
$fixturesObjects
=
$this
->
createFixtures
(
$fixtures
);
$this
->
unloadFixtures
(
$fixturesObjects
);
$this
->
loadFixtures
(
$fixturesObjects
);
$this
->
notifyLoaded
(
$fixtures
);
}
/**
* 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
* whitespace between
tables
names.
* whitespace between names.
* @param array|string $fixtures
* @param array|string $except
*/
...
...
@@ -169,43 +141,17 @@ class FixtureController extends Controller
}
$filtered
=
array_diff
(
$foundFixtures
,
$except
);
$fixtures
=
$this
->
getFixturesConfig
(
array_merge
(
$this
->
globalFixtures
,
$filtered
));
$fixtures
=
$this
->
getFixturesConfig
(
array_merge
(
$this
->
globalFixtures
,
$filtered
));
if
(
!
$fixtures
)
{
throw
new
Exception
(
'No fixtures were found in namespace: '
.
$this
->
namespace
.
'".'
);
}
$transaction
=
$this
->
getDbConnection
()
->
beginTransaction
();
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
->
unloadFixtures
(
$this
->
createFixtures
(
$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.
* @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