禪道項目管理(ZenTao)PHP框架提供的DAO功能
禪道項目管理軟件(www.zentao.net)并沒有試著去實現ORM或者ActiveRecord這樣的概念。因為我們相信,框架要留給開發人員足夠的自由發揮的空間,而不是所有的都要包辦。所以框架里面提供了一個簡單方便的數據庫訪問對象類:dao,讓我們來看具體的寫法。
一、查詢語句:
$this->dao->select('*')->from('user')->where('account')->eq('wwccss')->fetch();
$this->dao->select('*')->from('user')->where('id')->gt(10)->andWhere('age')->lt(20)->orderBy('id desc')->limit('1,10')->fetchAll()
條件語句:
$this->dao->select('*')->from('user')->where('id')->gt(10)->beginIF($class == 'online')->andWhere('status')->eq('online')->fi()->fetchAll();
二、插入語句:
$user->account = 'wwccss';
$user->password = '123456';
$this->dao->insert('user')->data($user)->exec();
return $this->dao->lastInsertID();
或者:
$this->dao->insert('user')
->set('account')->eq($account)
->set('password')->eq($password)
->exec();
三、更新語句:
$this->dao->update('user')->data($user)->where('id')->eq($userid)->limit(1); 或者:
$this->dao->update('user')
->set('account')->eq($account)
->set('password')->eq($password)
->exec()
四、REPLACE語句
$this->dao->replace('user')->data($user)->exec();
五、刪除語句:
$this->dao->delete()->from('user')->where('id')->eq($userid);
六、左連接
$this->dao->select('t1.*, t2.*')->from('user')->alias('t1')->leftJoin('userGroup')->alias('t2')->on('t1.account = t2.account')->fetchAll();
六、其他便利的方法:
$this->dao->findByAccount($account)->from('user')->fetch(); // 魔術方法,按照account進行查詢。
$this->dao->select('*')->from('user')->fetchAll('account'); // 返回的結果中,以account為key。
$this->dao->select('account, realname')->from('user')->fetchPairs(); // 返回account=>realname的鍵值對。
$this->dao->select('class, account, realname')->from('user')->fetchGroup('class'); // 按照所屬的class進行分組。