Zend Framework数据库操作

 1 /**
 2      * 给Zend_Db_Select增加where条件
 3      * 
 4      * @param Zend_Db_Select $select
 5      * @param array $where
 6      * @return Zend_Db_Select
 7      */
 8     protected function addWhereToSelect($select, $where) {
 9         foreach ($where as $k=>$v) {
10             if ($v === null) {
11                 $select->where($k);
12             } else {
13                 $select->where($k, $v);
14             }
15         }
16         if ($this->_status_fieldname) {
17             $select->where($this->_status_fieldname . ' < ?', Mana_Const::STATUS_DEL);
18         }
19         return $select;
20     }

 

 1 /**
 2      * 给Zend_Db_Select增加where条件
 3      * 
 4      * @param Zend_Db_Select $select
 5      * @param array $where
 6      * @return Zend_Db_Select
 7      */
 8     protected function addWhereToSelect($select, $where) {
 9         foreach ($where as $k=>$v) {
10             if ($v === null) {
11                 $select->where($k);
12             } else {
13                 $select->where($k, $v);
14             }
15         }
16         if ($this->_status_fieldname) {
17             $select->where($this->_status_fieldname . ' < ?', Mana_Const::STATUS_DEL);
18         }
19         return $select;
20     }
 1 /**
 2      * Update delete等增加where条件
 3      * 
 4      * @param array $where
 5      * @return string
 6      */
 7     protected function addWhereToUdl($where) {
 8         $sw = '';
 9         foreach ($where as $quote=>$val) {
10             if ($val === null) {
11                 $sw .= ' ' . $quote . ' AND';
12             } else {
13                 $sw .= ' ' .$this->_db->quoteInto($quote, $val) . ' AND';
14             }
15         }
16         if ($sw) {
17             $sw = trim($sw, 'AND');
18         }
19         return $sw;
20     }
 1 /**
 2      * 
 3      * 获得满足条件的第一行数据
 4      * @param array $mixWhere
 5      * 设置连表情况
 6      *
 7      * @param array $options like
 8      * array(
 9      *     tables=>array('key'=>array('tablename'=>'', 'on'=>'', 'fields'=>array())),
10      *     'where'=>array(
11      *         'tbl.field = ?'=>val,
12      *     )
13      * )
14      */
15     public function getRow($mixWhere = array(), $options = array()) {
16         $objSelect = $this->_db->select();        
17         $objSelect->from(array('s'=>$this->getTableName()),'*');
18         $objSelect = $this->addWhereToSelect($objSelect, $mixWhere);
19         $objSelect = $this->addOptionsToSelect($objSelect, $options);
20         $this->_logger->log(__CLASS__ . '.'.__FUNCTION__ . '.'.$objSelect->__toString(), Zend_Log::INFO);
21         $objRow = $this->_db->fetchRow($objSelect,null,Zend_Db::FETCH_OBJ);
22         return $objRow;
23     }
 1 /**
 2      * 获得满足条件记录数量
 3      * 
 4      * @param array $mixWhere
 5      * 设置连表情况
 6      *
 7      * @param array $options like
 8      * array(
 9      *     tables=>array('key'=>array('tablename'=>'', 'on'=>'', 'fields'=>array())),
10      *     'where'=>array(
11      *         'tbl.field = ?'=>val,
12      *     )
13      * )
14      * @return int
15      */
16     public function getCount($mixWhere = array(), $options = array()) {
17         $objSelect = $this->_db->select();        
18         $objSelect->from(array('s'=>$this->getTableName()),array('COUNT(1)'));
19         $objSelect = $this->addWhereToSelect($objSelect, $mixWhere);
20         $objSelect = $this->addOptionsToSelect($objSelect, $options);
21         $this->_logger->log(__CLASS__ . '.'.__FUNCTION__ . '.'.$objSelect->__toString(), Zend_Log::INFO);
22         $num = $this->_db->fetchOne($objSelect,null,Zend_Db::FETCH_OBJ);
23         return $num;
24     }
25     

 

 1 /**
 2      * 
 3      * Enter description here ...
 4      * @param Zend_Db_Select $select
 5      * @param unknown_type $options
 6      * 设置连表情况
 7      *
 8      * @param array $options like
 9      * array(
10      *     tables=>array('key'=>array('tablename'=>'', 'on'=>'', 'fields'=>array(), 'jointype'=>'join|joinleft|joinright')),
11      *     'where'=>array(
12      *         'tbl.field = ?'=>val,
13      *     )
14      * )
15      * @return Zend_Db_Select
16      */
17     protected function addOptionsToSelect($select, $options) {
18         if (empty($options)) {
19             return $select;
20         }
21         $tables = $options['tables'];
22         foreach ($tables as $key=>$tblJoinInfo) {
23             $tName = '';
24             if (is_numeric($key)) {
25                 $tName = $tblJoinInfo['tablename'];
26             } else {
27                 $tName = array($key=>$tblJoinInfo['tablename']);
28             }
29             if ($tblJoinInfo['jointype'] == '' || $tblJoinInfo['jointype'] == 'join') {
30                 $select->join($tName, $tblJoinInfo['on'], $tblJoinInfo['fields']);
31             } elseif ($tblJoinInfo['jointype'] == 'joinleft') {
32                 $select->joinLeft($tName, $tblJoinInfo['on'], $tblJoinInfo['fields']);
33             } elseif ($tblJoinInfo['jointype'] == 'joinright') {
34                 $select->joinRight($tName, $tblJoinInfo['on'], $tblJoinInfo['fields']);
35             } else {
36                 throw new Exception('查询错误', 90001);
37             }
38         }
39         $wheres = $options['where'];
40         if ($wheres) {
41             $select = $this->addWhereToSelect($select, $wheres);
42         }
43         return $select;
44     }

 

 1 /**
 2      * 
 3      * 获得满足条件的所有行数据
 4      * @param array $mixWhere
 5      * 设置连表情况
 6      *
 7      * @param array $options like
 8      * array(
 9      *     tables=>array('key'=>array('tablename'=>'', 'on'=>'', 'fields'=>array())),
10      *     'where'=>array(
11      *         'tbl.field = ?'=>val,
12      *     )
13      * )
14      */
15     public function getRows($mixWhere = array(), $options = array()) {
16         $objSelect = $this->_db->select();
17         $objSelect->from(array('s'=>$this->getTableName()),'*');
18         $objSelect = $this->addWhereToSelect($objSelect, $mixWhere);
19         $objSelect = $this->addOptionsToSelect($objSelect, $options);
20         $this->_logger->log(__CLASS__ . '.'.__FUNCTION__ . '.'.$objSelect->__toString(), Zend_Log::INFO);
21         $objRows = $this->_db->fetchAll($objSelect,null,Zend_Db::FETCH_OBJ);
22         return $objRows;
23     }

 

posted @ 2012-06-11 22:21  绒花雪冷  阅读(681)  评论(0编辑  收藏  举报