From: Andrey Kutejko Date: Thu, 13 Jun 2013 19:41:38 +0000 (+0300) Subject: simple fixtures X-Git-Tag: 0.5~248 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=bbf8e8a45825e5245724b18e863d4a4949d730c0;p=ipf.git simple fixtures --- diff --git a/ipf/cli.php b/ipf/cli.php index 00e2132..14f480d 100644 --- a/ipf/cli.php +++ b/ipf/cli.php @@ -5,7 +5,7 @@ class IPF_Cli{ protected $commands; public function __construct(){ - $this->commands = array('help','sql','buildmodels','buildcontribmodels','syncdb', 'createsuperuser', 'syncperms'); + $this->commands = array('help','sql','buildmodels','buildcontribmodels','syncdb', 'createsuperuser', 'syncperms', 'fixtures'); } protected function usage(&$args){ @@ -58,7 +58,7 @@ class IPF_Cli{ $username = ''; while ($username==''){ print " Username: "; $username = trim(fgets(STDIN)); }; $password = ''; while ($password==''){ print " Password: "; $password = trim(fgets(STDIN)); }; $email = ''; while ($email==''){ print " e-mail: "; $email = trim(fgets(STDIN)); }; - + $su = new User(); $su->username = $username; $su->email = $email; @@ -70,27 +70,32 @@ class IPF_Cli{ print "Done\n"; } - public function run(){ - + protected function fixtures(&$args) + { + print "Load project fixtures to database\n"; + IPF_Project::getInstance()->loadFixtures(); + } + + public function run() + { print "IPF command line tool. Version: ".IPF_Version::$name."\n"; print "Project config: ".IPF::get('settings_file')."\n"; - + $opt = new IPF_Getopt(); //$z = $opt->getopt2($opt->readPHPArgv(), array('s',)); //, array('s',)); $args = $opt->readPHPArgv(); - if (count($args)==1) - { + if (count($args) == 1) { $this->usage($args); return; } - if (in_array($args[1],$this->commands)) - { + if (in_array($args[1], $this->commands)) { eval('$this->'.$args[1].'($args);'); return; } - + print "Unknown command: '".$args[1]."'\n"; $this->usage($args); } } + diff --git a/ipf/project.php b/ipf/project.php index 342e731..a2b3679 100644 --- a/ipf/project.php +++ b/ipf/project.php @@ -101,6 +101,45 @@ final class IPF_Project{ return $sql; } + public function loadFixtures() + { + $ficturesPath = IPF::get('project_path').DIRECTORY_SEPARATOR.'fixtures.php'; + if (!is_file($ficturesPath)) { + echo "No fixtures found\n"; + return; + } + + $this->loadModels(); + + $fixtures = require $ficturesPath; + + foreach ($fixtures as $fixture) { + $modelClass = $fixture['model']; + $key = $fixture['key']; + $records = $fixture['records']; + echo "Loading $modelClass "; + foreach ($records as $record) { + $model = IPF_ORM::getTable($modelClass) + ->createQuery() + ->where($key . ' = ?', array($record[$key])) + ->limit(1) + ->execute(); + + if ($model) + $model = $model[0]; + else + $model = new $modelClass; + + foreach ($record as $k => $v) + $model->$k = $v; + + $model->save(); + echo '.'; + } + echo "\n"; + } + } + public function loadModels(){ foreach( $this->apps as $appname=>&$app){ if (substr($appname,0,4)=='IPF_')