Created
October 29, 2012 16:34
-
-
Save kechol/3974699 to your computer and use it in GitHub Desktop.
seed shell for CakePHP 2.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class AppSeed { | |
| public $users = array( | |
| array( | |
| 'id' => 1, | |
| 'username' => 'testuser1', | |
| 'message' => 'Lorem ipsum dolor sit amet', | |
| ), | |
| array( | |
| 'id' => 2, | |
| 'username' => 'testuser2', | |
| 'message' => 'Lorem ipsum dolor sit amet', | |
| ), | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| App::uses('File', 'Utility'); | |
| App::uses('Folder', 'Utility'); | |
| class SeedShell extends AppShell { | |
| public function startup() { | |
| $this->_welcome(); | |
| $this->out('Cake Seed Shell'); | |
| $this->hr(); | |
| if (empty($this->params['name'])) { | |
| $this->params['name'] = 'app'; | |
| } | |
| if (empty($this->params['file'])) { | |
| $this->params['file'] = 'seed.php'; | |
| } | |
| if (strpos($this->params['file'], '.php') === false) { | |
| $this->params['file'] .= '.php'; | |
| } | |
| if (empty($this->params['path'])) { | |
| $this->params['path'] = APP . 'Config'; | |
| } | |
| if (empty($this->params['connection'])) { | |
| $this->params['connection'] = 'default'; | |
| } | |
| $file = $this->params['file']; | |
| $path = $this->params['path']; | |
| $name = Inflector::camelize($this->params['name']); | |
| $this->connection = $this->params['connection']; | |
| if(file_exists($path . DS . $file) | |
| && is_file($path . DS . $file)) { | |
| require_once $path . DS . $file; | |
| } else { | |
| $this->out('seed file is not exist.'); | |
| exit(); | |
| } | |
| $class = $name . 'Seed'; | |
| if(class_exists($class)) { | |
| $this->seed = new $class(); | |
| } | |
| } | |
| public function create() { | |
| $models = App::objects('Model'); | |
| if(is_array($models)) { | |
| foreach($models as $model) { | |
| if ($model == 'AppModel') { | |
| continue; | |
| } | |
| try { | |
| $Object = ClassRegistry::init(array('class' => $model, 'ds' => $this->connection)); | |
| } catch (CakeException $e) { | |
| continue; | |
| } | |
| if (is_object($Object) && $Object->useTable !== false) { | |
| $table = $Object->table; | |
| if(isset($this->seed->$table)) { | |
| $rows = count($this->seed->$table); | |
| $this->out($rows . ' rows added to ' . $table); | |
| $Object->saveMany($this->seed->$table); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| public function delete() { | |
| $models = App::objects('Model'); | |
| if(is_array($models)) { | |
| foreach($models as $model) { | |
| if ($model == 'AppModel') { | |
| continue; | |
| } | |
| try { | |
| $Object = ClassRegistry::init(array('class' => $model, 'ds' => $this->connection)); | |
| } catch (CakeException $e) { | |
| continue; | |
| } | |
| if (is_object($Object) && $Object->useTable !== false) { | |
| $this->out('deleted all data in ' . $Object->table); | |
| $Object->deleteAll('1 = 1'); | |
| } | |
| } | |
| } | |
| } | |
| public function getOptionParser() { | |
| $connection = array( | |
| 'short' => 'c', | |
| 'help' => __d('cake_console', 'Set the db config to use.'), | |
| 'default' => 'default' | |
| ); | |
| $path = array( | |
| 'help' => __d('cake_console', 'Path to read seed.php'), | |
| 'default' => APP . 'Config' | |
| ); | |
| $file = array( | |
| 'help' => __d('cake_console', 'File name to read seeds.'), | |
| 'default' => 'seed.php' | |
| ); | |
| $name = array( | |
| 'help' => __d('cake_console', 'Classname to use.') | |
| ); | |
| $parser = parent::getOptionParser(); | |
| $parser->description( | |
| __d('cake_console', 'The Seed Shell adds seed data to database tables from a seed file.') | |
| )->addSubcommand('create', array( | |
| 'help' => __d('cake_console', 'add seed data to tables.'), | |
| 'parser' => array( | |
| 'options' => compact('path', 'file', 'name', 'connection'), | |
| 'args' => array( | |
| /* | |
| 'name' => array( | |
| 'help' => __d('cake_console', 'Name of schema to use.') | |
| ), | |
| 'table' => array( | |
| 'help' => __d('cake_console', 'Only create the specified table.') | |
| ) | |
| */ | |
| ) | |
| ) | |
| ))->addSubcommand('delete', array( | |
| 'help' => __d('cake_console', 'delete all data from tables'), | |
| 'parser' => array( | |
| 'options' => compact('connection'), | |
| 'args' => array( | |
| ) | |
| ) | |
| )); | |
| return $parser; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this looks great - where do you install it and how do you invoke it?