Callbacks

Version 3 (San Asol, 2015-01-27 07:56 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 3 San Asol
*before_destroy*: called before 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 2 Kien La
$attributes = array('item_name' => 'Honda Civic',
42 2 Kien La
  'price' => 7000.00, 'state' => 'va');
43 1
 
44 1
$order = Order::create($attributes);
45 1
echo $order->tax; # => 315.00
46 1
echo $order->state; # => VA
47 1
48 1
# somehow our order changed states!
49 1
$order->state = 'nc';
50 1
$order->save();
51 1
echo $order->state; # => NC
52 1
</code></pre>
53 1
 
54 1
h4(#after-callbacks). After callbacks
55 1
56 1
*after_save*: called after a model is saved
57 1
*after_create*: called after a NEW model has been inserted into the database
58 1
*after_update*: called after an existing model has been saved
59 1
*after_validation*: called after running validators
60 1
*after_validation_on_create*: called after validation on a NEW model being inserted
61 1
*after_validation_on_update*: same as above except for an existing model being saved
62 1
*after_destroy*: called after a model has been deleted
63 1
64 1
<pre class="code"><code class="php">
65 1
class User extends ActiveRecord\Model {
66 1
  static $after_create = array('send_new_user_email'); # new records only
67 1
  static $after_destroy = array('delete_all_related_data');
68 1
 
69 1
  public function delete_all_related_data() {
70 1
    # delete all associated objects
71 1
  }
72 1
 
73 1
  public function send_new_user_email() {
74 2 Kien La
    mail($this->email, "Thanks for signing up, {$this->name}!", "The subject");
75 1
  }
76 1
}
77 1
 
78 1
$attributes = array('name' => 'Jax', 'email' => '[email protected]');
79 1
$user = User::create($attributes);
80 1
# an e-mail was just sent...
81 1
82 1
$user->delete();
83 1
# everything associated with this user was just deleted
84 1
</code></pre>