As
previously mentioned, I have split out the Fixture part of my migrations script and placed it in its own bake2 task.
The script below is pretty much the same as the previous version of fixtures. Just run the following:
php bake2.php fixture app users
Replace "app" with your applications alias (as defined in the apps.ini file). And "users" is the name of the table that you want to run the fixture for.
/**
* The FixtureTask runs a specified database fixture.
*
* PHP versions 4 and 5
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2006-2007, Joel Moss
* @link http://joelmoss.info
* @package cake
* @subpackage cake.cake.scripts.bake
* @since CakePHP(tm) v 1.2
* @version $Version: 3.0 $
* @modifiedby $LastChangedBy: joelmoss $
* @lastmodified $Date: 2007-02-16 09:09:45 +0000 (Fri, 16 Feb 2007) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
uses('file', 'folder');
class FixtureTask extends BakeTask
{
function execute($params)
{
$this->welcome();
if ($params[0] == 'help')
{
$this->help();
exit;
}
$this->initDatabase();
$this->initApp();
if (count($params) > 0)
{
define('FIXTURES_PATH', APP_PATH .DS. 'config' .DS. 'fixtures');
$this->fixture = $params[0];
$this->fixtures();
exit;
}
else
{
$this->err('Table name not specified.');
}
$this->help();
}
function fixtures()
{
if (!file_exists(FIXTURES_PATH)) $folder = new Folder(FIXTURES_PATH, true, 0777);
$tables = $this->_db->listTables();
if (!count($tables))
$this->err('Database contains no tables. Please run your migrations before your fixtures.');
if (!file_exists(FIXTURES_PATH .DS. $this->fixture .'.yml'))
{
$this->err('Fixture does not exist for table ''.$this->fixture.''.');
}
$this->out('');
$this->out(" Running fixture for '".$this->fixture."' ... ", false);
$this->startFixture($this->fixture);
$this->out('Fixture completed.');
$this->hr();
}
function startFixture($name)
{
$file = FIXTURES_PATH .DS. $name .'.yml';
if (function_exists('syck_load'))
{
$yml = file_get_contents($file);
$data = @syck_load($yml);
}
else
{
vendor('Spyc');
$data = Spyc::YAMLLoad($file);
}
if (!is_array($data) || !count($data))
$this->err("Unable to parse YAML Fixture file: '$file'");
$res = $this->_db->query("DELETE FROM `$name`");
if (PEAR::isError($res)) $this->err($res->getDebugInfo());
$count = 0;
foreach($data as $ri=>$r)
{
#if (!in_array('created', $r)) $data[$ri]['created'] = date('Y-m-d H:i:s');
foreach($r as $fi=>$f)
if ($f == 'NOW') $data[$ri][$fi] = date('Y-m-d H:i:s');
$res = $this->_db->autoExecute($name, $data[$ri], MDB2_AUTOQUERY_INSERT);
if (PEAR::isError($res)) $this->err($res->getDebugInfo());
$count = $count+$res;
}
$this->out("($count rows) ... ", false);
}
function initApp()
{
$this->out("Application: '".APP_DIR."' (".APP_PATH.")");
$this->hr();
}
function initDatabase()
{
if (!@include_once('MDB2.php'))
{
echo "nnTask Error: Unable to include PEAR.php and MDB2.phpnn";
exit;
}
if(!file_exists(APP_PATH.'config'.DS.'database.php'))
{
$this->out('** Checking for database configuration ... NOT FOUND! **');
$this->out();
$this->out('IMPORTANT!');
$this->out('Your database configuration ('.APP_PATH.'config'.DS.'database.php) was not found. Please');
$this->out('take a moment to create one by running the 'dbconfig' task:');
$this->out('');
$this->out(' php bake2.php dbconfig [...]');
$this->hr();
exit;
}
require_once (APP_PATH . 'config' .DS. 'database.php');
$ds = new DATABASE_CONFIG();
$config = $ds->default;
$dsn = array(
'phptype' => $config['driver'],
'username' => $config['login'],
'password' => $config['password'],
'hostspec' => $config['host'],
'database' => $config['database']
);
$options = array(
'debug' => DEBUG,
'portability' => DB_PORTABILITY_ALL
);
$this->_db = &MDB2::connect($dsn, $options);
if (PEAR::isError($this->_db)) $this->err($this->_db->getDebugInfo());
$this->_db->setFetchMode(MDB2_FETCHMODE_ASSOC);
$this->_db->loadModule('Manager');
$this->_db->loadModule('Extended');
$this->_db->loadModule('Reverse');
}
function help()
{
echo "This task inserts database fixtures.n";
echo "Usage: bake2 fixture app_alias [table_name]n";
}
function out($str='', $newline=true)
{
$nl = $newline ? "n" : "";
echo " $str$nl";
}
function hr()
{
echo "n ----------------------------------------------------------------------------n";
}
function err($str)
{
$this->out('');
$this->out(' ** '.$str.' **');
$this->hr();
exit;
}
function welcome()
{
$this->out('');
$this->hr();
$this->out(' __ __ _ _ __ __ _ _ __ __ ___ _ __ _ ');
$this->out('| |__| |_/ |__ |__] |__| |__] |_ | / | | | |_] |__ [_ ');
$this->out('|__ | | | _ |__ | | | | | | / | |_| | |__ _]');
$this->hr();
$this->out('');
}
}
That is pretty much it.