Skip to content

Loading…

Added example and some doc for CakeSchema callbacks #249

Merged
merged 4 commits into from

3 participants

@huoxito

No description provided.

@lorenzo lorenzo commented on an outdated diff
...nsole-and-shells/schema-management-and-migrations.rst
((11 lines not shown))
+
+The ``$event`` param holds an array with two keys. One to tell if a
+table is being dropped or created and another for errors. Examples::
+
+ array('drop' => 'posts', 'errors' => null)
+ array('create' => 'posts', 'errors' => null)
+
+Adding data to a posts table for example would like this::
+
+ <?php
+ App::uses('Post', 'Model');
+ public function after($event = array()) {
+ if(isset($event['create'])){
+ switch($event['create']){
+ case 'posts':
+ $this->Post = new Post();
@lorenzo CakePHP member
lorenzo added a note

It is usually better to use ClassRegistry. Also you should take into consideration the connection that was passed to the schema, models will use the default connection if no parameters are passed.

@huoxito
huoxito added a note

Added commit to use ClassRegistry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@huoxito

Hey @lorenzo also added the connection property to the example. Check if thats ok now.

@markstory markstory commented on an outdated diff
...nsole-and-shells/schema-management-and-migrations.rst
((11 lines not shown))
+
+The ``$event`` param holds an array with two keys. One to tell if a
+table is being dropped or created and another for errors. Examples::
+
+ array('drop' => 'posts', 'errors' => null)
+ array('create' => 'posts', 'errors' => null)
+
+Adding data to a posts table for example would like this::
+
+ <?php
+ App::uses('Post', 'Model');
+ public function after($event = array()) {
+ if(isset($event['create'])){
+ switch($event['create']){
+ case 'posts':
+ $this->Post = ClassRegistry::init('Post');
@markstory CakePHP member

Why not just make a local variable? The object property isn't reused at all. A local variable will be garbage collected at least.

@huoxito
huoxito added a note

My mistake, added commit to make that var local

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory merged commit f572fc0 into cakephp:master
@markstory
CakePHP member

Thanks again :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 50 additions and 0 deletions.
  1. +50 −0 en/console-and-shells/schema-management-and-migrations.rst
View
50 en/console-and-shells/schema-management-and-migrations.rst
@@ -41,6 +41,56 @@ Where filename.sql is the desired filename for the sql dump. If you
omit filename.sql the sql dump will be output to the console but
not written to a file.
+CakeSchema callbacks
+====================
+
+After generating a schema you might want to insert data on some
+tables to get your app started. This can be achieved through
+CakeSchema callbacks. Every schema file is generated with a
+``before($event = array())`` and a ``after($event = array())`` method.
+
+The ``$event`` param holds an array with two keys. One to tell if a
+table is being dropped or created and another for errors. Examples::
+
+ array('drop' => 'posts', 'errors' => null)
+ array('create' => 'posts', 'errors' => null)
+
+Adding data to a posts table for example would like this::
+
+ <?php
+ App::uses('Post', 'Model');
+ public function after($event = array()) {
+ if(isset($event['create'])){
+ switch($event['create']){
+ case 'posts':
+ $post = ClassRegistry::init('Post');
+ $post->create();
+ $post->save(
+ array('Post' =>
+ array('title' => 'CakePHP Schema Files')
+ )
+ );
+ break;
+ }
+ }
+ }
+
+The ``before()`` and ``after()`` callbacks run each time a table is created
+or dropped on the current schema.
+
+When inserting data to more than one table you'll need to flush the database
+cache after each table is created. Cache can be disable by setting
+``$db->cacheSources = false`` in the before action(). ::
+
+ <?php
+ public $connection = 'default';
+
+ public function before($event = array()) {
+ $db = ConnectionManager::getDataSource($this->connection);
+ $db->cacheSources = false;
+ return true;
+ }
+
Migrations with CakePHP schema shell
====================================
Something went wrong with that request. Please try again.