php如何设计自定义模型,php – 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;
該方法計算用戶的信用(可以是信用和 – 信用)并返回可用信用總額.只要我使用ORM :: factory方法創建用戶對象,我就想以這樣的方式實現’total’方法,它使用加載的資源而不是運行新的查詢(我寫了上面的查詢來演示業務邏輯).
有什么建議?
標簽:php,orm,model,kohana,kohana-3
來源: https://codeday.me/bug/20190531/1188652.html
總結
以上是生活随笔為你收集整理的php如何设计自定义模型,php – Kohana ORM模型中的自定义方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最拼爹的css属性:z-index失效情
- 下一篇: 动态规划算法php,php算法学习之动态