/var/www/(Del)gepgroup.hk/php-activerecord/examples/orders/models/Order.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
class Order extends ActiveRecord\Model
{
    
// order belongs to a person
    
static $belongs_to = array(
        array(
'person'));

    
// order can have many payments by many people
    // the conditions is just there as an example as it makes no logical sense
    
static $has_many = array(
        array(
'payments'),
        array(
'people',
            
'through'    => 'payments',
            
'select'     => 'people.*, payments.amount',
            
'conditions' => 'payments.amount < 200'));

    
// order must have a price and tax > 0
    
static $validates_numericality_of = array(
        array(
'price''greater_than' => 0),
        array(
'tax',   'greater_than' => 0));

    
// setup a callback to automatically apply a tax
    
static $before_validation_on_create = array('apply_tax');

    public function 
apply_tax()
    {
        if (
$this->person->state == 'VA')
            
$tax 0.045;
        elseif (
$this->person->state == 'CA')
            
$tax 0.10;
        else
            
$tax 0.02;

        
$this->tax $this->price $tax;
    }
}
?>