Callbacks

Version 1 (Kien La, 2010-06-19 06:23 PM)

1 1
h2. Callbacks
2 1
3 1
*(#topic-list) "Before callbacks":/projects/main/wiki/Callbacks#before-callbacks
4 1
* "After callbacks":/projects/main/wiki/Callbacks#after-callbacks
5 1
6 1
Callbacks allow the programmer to hook into the life cycle of an ActiveRecord\Model object. You can control the state of your object by declaring certain methods to be called before or after methods are invoked on your object inside of ActiveRecord.
7 1
8 1
h4(#before-callbacks). Before callbacks
9 1
10 1
If a before_* callback returns false, execution of any other callbacks after the offending callback will not be fired and the model will not be saved/deleted.
11 1
12 1
*before_save*: called before a model is saved
13 1
*before_create*: called before a NEW model is to be inserted into the database
14 1
*before_update*: called before an existing model has been saved
15 1
*before_validation*: called before running validators
16 1
*before_validation_on_create*: called before validation on a NEW model being inserted
17 1
*before_validation_on_update*: same as above except for an existing model being saved
18 1
*before_destroy*: called after a model has been deleted
19 1
20 1
<pre class="code"><code class="php">
21 1
class Order extends ActiveRecord\Model {
22 1
  static $before_create = array('apply_tax'); # new records only
23 1
  static $before_save = array('upcase_state'); # new OR updated records
24 1
25 1
  public function apply_tax() {
26 1
    if ($this->state == 'VA')
27 1
      $tax = 0.045;
28 1
    elseif ($this->state == 'CA')
29 1
      $tax = 0.10;
30 1
    else
31 1
      $tax = 0.02;
32 1
 
33 1
    $this->tax = $this->price * $tax;
34 1
  }
35 1
 
36 1
  public function upcase_state() {
37 1
    $this->state = strtoupper($this->state);
38 1
  }
39 1
}
40 1
 
41 1
$attributes = array('item_name' => 'Honda Civic', 'price' => 7000.00, 'state' => 'va');
42 1
 
43 1
$order = Order::create($attributes);
44 1
echo $order->tax; # => 315.00
45 1
echo $order->state; # => VA
46 1
47 1
# somehow our order changed states!
48 1
$order->state = 'nc';
49 1
$order->save();
50 1
echo $order->state; # => NC
51 1
</code></pre>
52 1
 
53 1
h4(#after-callbacks). After callbacks
54 1
55 1
*after_save*: called after a model is saved
56 1
*after_create*: called after a NEW model has been inserted into the database
57 1
*after_update*: called after an existing model has been saved
58 1
*after_validation*: called after running validators
59 1
*after_validation_on_create*: called after validation on a NEW model being inserted
60 1
*after_validation_on_update*: same as above except for an existing model being saved
61 1
*after_destroy*: called after a model has been deleted
62 1
63 1
<pre class="code"><code class="php">
64 1
class User extends ActiveRecord\Model {
65 1
  static $after_create = array('send_new_user_email'); # new records only
66 1
  static $after_destroy = array('delete_all_related_data');
67 1
 
68 1
  public function delete_all_related_data() {
69 1
    # delete all associated objects
70 1
  }
71 1
 
72 1
  public function send_new_user_email() {
73 1
    mail($this->email, "Thanks for signing up, {$this->name}!", "Awesome subject");
74 1
  }
75 1
}
76 1
 
77 1
$attributes = array('name' => 'Jax', 'email' => 'nonya@nonya.com');
78 1
$user = User::create($attributes);
79 1
# an e-mail was just sent...
80 1
81 1
$user->delete();
82 1
# everything associated with this user was just deleted
83 1
</code></pre>