Skip to content

Instantly share code, notes, and snippets.

@sime
Created February 26, 2012 15:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sime/1917192 to your computer and use it in GitHub Desktop.
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
<?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' => 'joe@example.com'),
array ('firstname' => 'Fred', 'lastname' => 'Bloggs', 'email' => 'fred@exampe.com'),
);
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