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){
$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;
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);
}
}
+
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_')