4
我有這兩種模式:在模型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();
的事情是如何實現「總」方法,它的確是這樣的:
return DB::select(array(DB::expr('SUM(qty * sign)'), 'total')) ->from($this->_tb) ->where('user', '=', $user_id) ->execute() ->total;
的方法計算用戶的學分(可以是+學分和 - 學分)並返回可用學分總數。只要我使用ORM :: factory方法創建用戶對象,我想以這種方式實現'total'方法,它使用加載的資源而不是運行新的查詢(我寫了上面的查詢來演示業務邏輯)。
有什麼建議嗎? :)
酷,工作正常!非常感謝,科莫 – 2011-01-31 13:59:01