小编sir*_*sek的帖子

Kohana ORM模型中的自定义方法

我有这两个模型:

class Model_user extends ORM {
    protected $_has_many = array('credits', array('model'=>'credit', 'foreign_key'=>'user'));
}

class Model_credit extends ORM {
    protected $_belongs_to = array('user', array('model'=>'user', 'foreign_key'=>'user'));
    protected $_tb = 'credits';
    protected $_pk = 'id'; 
    // method to be implemented
    public function total() {
        return $total_credits_available;
    }
}

// accessing the 'total' method
$user = ORM::factory('user', 1);
$total_credits = $user->credits->total();

问题是如何实现'total'方法,它具有以下特点:

return DB::select(array(DB::expr('SUM(qty * sign)'), 'total'))
    ->from($this->_tb)
    ->where('user', '=', $user_id)
    ->execute()
    ->total; …

php orm model kohana kohana-3

4
推荐指数
1
解决办法
3036
查看次数

标签 统计

kohana ×1

kohana-3 ×1

model ×1

orm ×1

php ×1