另一种方法是通过sparks使用php-activerecord
协会的一个例子
-
class User extends ActiveRecord\Model{
//set tablename
//I would advice to keep Model singular and table names plural
//then this next step is not needed
static $table_name = 'table_one';
//set relationships
static $belongs_to array(
array('Group')
);
static $has_many = array(
array('Comment'),
array('Order')
);
static $has_one = array(
array('Additional_Info')
);
//one key feature is callbacks
//this helps keep controllers clean by letting you pass
//the post data(after validation) in to modify(serialize,json_encode,calculate vat etc)
static $before_create = array('json_encode_my_tags');
public function json_encode_my_tags(){
//$this->tags === $this->input->post('tags');
$tags = explode(',', str_replace(' ', '', $this->tags));
return $this->tags = json_encode($tags, TRUE);
}
}
//calling a model and using eager-loading for associations
$model = User::find(array(
'conditions' => array('id=?', $id) // PK Primary key
'include' => array('comment', 'order', 'additional_info') // eager-loading associations
));
$model->key;
$model->comment->key;
$model->additional_info->key;
$model->order->key;