助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
在任何一个网站系统中数据库的操作都是一个重心核心问题,在很多时候做为一个开发都有自已已经熟练使用的一套数据库操作类库,使用自已熟悉的类库不仅有助于提高开发效率,也有助于发现问题。在此作者根据自已的使用习惯套用ZF2中的相关数据库操作类库重写了一个实用模型。此节的内容重不在于类库本身,而是通过这个类库来扩展自已的思维,以便日后可以自已的需要重写自已使用的类库。 新建模型文件:`/module/Application/src/Application/Model/NewsModel.php`,文件的内容如下: ~~~ namespace Application\Model; use Zend\Db\Adapter\Adapter; use Zend\Db\Sql\Sql; use Zend\Db\ResultSet\ResultSet; class NewsModel { protected $adapter; /** * 构造函数 * @param Array $config 数据库连接配置 */ public function __construct($config=null) { if($config==null) $this->adapter = new Adapter(array( 'driver'=>'Pdo_Mysql', 'database'=>'test', 'hostname'=>'localhost', 'username'=>'root', 'password'=>'' )); else $this->adapter = new Adapter($config); } /** * 返回查询结果的第一行数据 * @param String $table 操作的数据表名 * @param String $where 查询条件 * @return Array */ public function fetchRow($table,$where=null){ $sql = "SELECT * FROM {$table}"; if($where!=null) $sql .= "WHERE {$where}"; $statement = $this->adapter->createStatement($sql); $result = $statement->execute(); return $result->current(); } /** * 返回查询的所有结果 * @param String $table 数据表名 * @param String $where 查询条件 * @return Array */ public function fetchAll($table,$where=null){ $sql = "SELECT * FROM {$table}"; if($where!=null) $sql .= "WHERE {$where}"; $stmt = $this->adapter->createStatement($sql); $stmt->prepare(); $result = $stmt->execute(); $resultset = new ResultSet; $resultset->initialize($result); $rows = array(); $rows = $resultset->toArray(); return $rows; } /** * 返回指定表的所有数据 * @param String $table 表名 * @return Array */ public function getTableRecords($table) { $sql = new Sql($this->adapter); $select = $sql->select(); $select->from($table); $stmt = $sql->prepareStatementForSqlObject($select); $result = $stmt->execute(); $resultSet = new ResultSet(); $resultSet->initialize($result); return $resultSet->toArray(); } /** * 插入数据到数据表 * @param String $table * @param Array $data * @return Int 返回受影响的行数 */ public function insert($table,$data){ $sql = new Sql($this->adapter); $insert=$sql->insert($table); $insert->values($data); return $sql->prepareStatementForSqlObject($insert)->execute()->getAffectedRows(); } /** * 更新数据表 * @param String $table 数据表名 * @param String $data 需要更新的数据 * @param String|Array $where 更新条件 * @return Int 返回受影响的行数 */ public function update($table,$data,$where){ $sql = new Sql($this->adapter); $update=$sql->update($table); $update->set($data); $update->where($where); return $sql->prepareStatementForSqlObject($update)->execute()->getAffectedRows(); } /** * 删除数据 * @param String $table 数据表名 * @param String|Array $where 删除条件 * @return Int 返回受影响的行数 */ public function delete($table,$where){ $sql = new Sql($this->adapter); $delete = $sql->delete($table)->where($where); return $sql->prepareStatementForSqlObject($delete)->execute()->getAffectedRows(); } /** * 返回最后插入的主键值 * @return Int */ public function lastInsertId(){ return $this->adapter->getDriver()->getLastGeneratedValue(); } } ~~~ 以上代码为一个完整的模型代码,这个模型中使用了多个ZF2中的DB类库来实现不能的功能需求,上面只是一个范例且已经对各个函数方法给出了注释,在此就不对该模型做一一详解。