Created
February 26, 2012 15:01
-
-
Save sime/1917192 to your computer and use it in GitHub Desktop.
Possible example of using the CakePHP Schema callback `after` to insert content in the database
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 | |
// Possible example of using the CakePHP Schema callback `after` | |
// to insert content in the database. | |
// Inspiration: https://github.com/majna/schema | |
// Copy schema.php to app/Config/Schema | |
// Run: ./Console/cake schema create | |
App::uses('ClassRegistry', 'Utility'); | |
class AppSchema extends CakeSchema { | |
public function before($event = array()) { | |
return true; | |
} | |
public function after($event = array()) { | |
if (isset($event['create'])) { | |
$table = $event['create']; | |
$data = null; | |
switch($table) { | |
case 'users': | |
$data = array( | |
array ('firstname' => 'Joe', 'lastname' => 'Bloggs', 'email' => '[email protected]'), | |
array ('firstname' => 'Fred', 'lastname' => 'Bloggs', 'email' => '[email protected]'), | |
); | |
break; | |
default: | |
} | |
if ($data) { | |
ClassRegistry::init($table)->saveAll($data); | |
} | |
} | |
} | |
public $users = array( | |
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 10, 'key' => 'primary', 'collate' => NULL, 'comment' => ''), | |
'firstname' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 100, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'), | |
'lastname' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 100, 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'), | |
'email' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100, 'key' => 'unique', 'collate' => 'utf8_general_ci', 'comment' => '', 'charset' => 'utf8'), | |
'indexes' => array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'email' => array('column' => 'email', 'unique' => 1)), | |
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB') | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment