最一开始设计zentaophp框架的时候,并没有考虑提供数据库访问抽象的功能。当时在做zentaopms最开始版本的时候,都是手工写的sql。不过手写sql还是比较繁琐的一件事情,尤其是对我这样完美性格的人来讲:)。
于是一个多月之前,开始动手写这个数据库访问的抽象层。最开始给这个抽象层定义为orm,后来写着写着觉得不对,自己写的这个东西仅仅是数据库访问的抽象,不是orm,所以后来将其改为DAO,data access object。
一、查询语句:
$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()
二、插入语句:
$user['account'] = 'wwccss';
$user['password'] = '123456';
$this->dao->insert('user')->data($user)->exec();
return $this->dao->lastInsertID();
三、更新语句:
$this->dao->update('user')->data($user)->where('id')->eq($userid)->limit(1);
四、删除语句:
$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();
$this->dao->select('*')->from('user')->fetchAll('account'); // 返回的结果中,以account为key。
$this->dao->select('account, realname')->from('user')->fetchPairs(); // 返回account=>realname的键值对。
更加具体的例子,大家可以参考zentaopms中的代码。