让ar执行queryall和queryrow方法返回数组
2013-12-06 17:23
176 查看
让ar执行queryall和queryrow方法返回数组
<?php
namespace common\components;
use \CActiveRecord;
use \Yii;
use \CDbConnection;
use \CDbException;
use common\helpers\ConnectionHelper;
class DBActiveRecord extends CActiveRecord{
public static $_dbConnections = array();
/**
* @return 数据配置组
*/
public static function dbActiveGroup()
{
return get_called_class();
}
public static function model($className=__CLASS__)
{
if ($className === __CLASS__ )
{
$className = get_called_class();
}
return parent::model($className);
}
/**
* 重写了 CActiveRecord的获取数据库连接,默认从公共配置里面读取连接配置
*
* @return CDbConnection the database connection used by active record.
*/
function getDbConnection()
{
$activeGroup = $this->dbActiveGroup();
if(isset(self::$_dbConnections[$activeGroup]))
{
return self::$_dbConnections[$activeGroup];
}
else
{
$dbConnection = ConnectionHelper::get($activeGroup);
if( ! empty($dbConnection) && $dbConnection instanceof CDbConnection)
{
self::$_dbConnections[$activeGroup] = $dbConnection;
return $dbConnection;
}
else
{
throw new CDbException(Yii::t('yii','Active Record requires a "'.$activeGroup.'" CDbConnection application component.'));
}
}
}
/**
* 获取数据库连接,静态方法
*
* @return 数据连接
*/
public static function getDb()
{
$className = get_called_class();
return ConnectionHelper::get($className::dbActiveGroup());
}
/**
* 执行数据库function
*
* @return 执行结果
*/
public static function callFunction($mysqlFunction, array $params=array())
{
$className = get_called_class();
$connection = $className::getDb();
switch ($connection->driverName)
{
case 'mysql':
return ConnectionHelper::callMysqlFunction($connection, $mysqlFunction, $params);
break;
default:
break;
}
return FALSE;
}
/**
* 执行存储过程
*
* @return 执行结果
*/
public static function callProcedure($mysqlProcedure, array $params=array())
{
$className = get_called_class();
$connection = $className::getDb();
switch ($connection->driverName)
{
case 'mysql':
return ConnectionHelper::callMysqlProcedure($className::getDb(), $mysqlProcedure, $params);
break;
default:
break;
}
return FALSE;
}
/**
* 获取字段rawName加表别名前缀,主要联表时候防止where条件中字段冲突用的
* @param string $columnName
* @return string
*/
public function getColumnRawName($columnName)
{
$prefix = $this->getTableAlias(true) . '.';
$columns = $this->tableSchema->columns;
if (isset($columns[$columnName]))
{
return $prefix.$columns[$columnName]->rawName;
}
else
{
return $columnName;
}
}
/**
*
* @param mixed $criteria
*/
public function queryAll($criteria = NULL)
{
if ( ! empty($criteria))
{
$this->getDbCriteria()->mergeWith($criteria);
}
$result = $this->getCommandBuilder()
->createFindCommand($this->tableSchema, $this->getDbCriteria())
->queryAll();
$this->setDbCriteria(NULL);
return $result;
}
public function queryRow($criteria = NULL)
{
if ($criteria != NULL)
{
$this->getDbCriteria()->mergeWith($criteria);
}
$result = $this->getCommandBuilder()
->createFindCommand($this->tableSchema, $this->getDbCriteria())
->queryRow();
$this->setDbCriteria(NULL);
return $result;
}
public function compare($column, $value, $partialMatch = FALSE, $operator = 'AND')
{
$criteria = new \CDbCriteria;
$column = $this->getColumnRawName($column);
if ($value === array())
{
$criteria->condition = "1 = 0";
}
else if ($value === '')
{
$criteria->condition = $column." = ''";
}
else
{
$criteria->compare($column, $value, $partialMatch, $operator, TRUE);
}
$this->getDbCriteria()->mergeWith($criteria);
return $this;
}
}
<?php
namespace common\components;
use \CActiveRecord;
use \Yii;
use \CDbConnection;
use \CDbException;
use common\helpers\ConnectionHelper;
class DBActiveRecord extends CActiveRecord{
public static $_dbConnections = array();
/**
* @return 数据配置组
*/
public static function dbActiveGroup()
{
return get_called_class();
}
public static function model($className=__CLASS__)
{
if ($className === __CLASS__ )
{
$className = get_called_class();
}
return parent::model($className);
}
/**
* 重写了 CActiveRecord的获取数据库连接,默认从公共配置里面读取连接配置
*
* @return CDbConnection the database connection used by active record.
*/
function getDbConnection()
{
$activeGroup = $this->dbActiveGroup();
if(isset(self::$_dbConnections[$activeGroup]))
{
return self::$_dbConnections[$activeGroup];
}
else
{
$dbConnection = ConnectionHelper::get($activeGroup);
if( ! empty($dbConnection) && $dbConnection instanceof CDbConnection)
{
self::$_dbConnections[$activeGroup] = $dbConnection;
return $dbConnection;
}
else
{
throw new CDbException(Yii::t('yii','Active Record requires a "'.$activeGroup.'" CDbConnection application component.'));
}
}
}
/**
* 获取数据库连接,静态方法
*
* @return 数据连接
*/
public static function getDb()
{
$className = get_called_class();
return ConnectionHelper::get($className::dbActiveGroup());
}
/**
* 执行数据库function
*
* @return 执行结果
*/
public static function callFunction($mysqlFunction, array $params=array())
{
$className = get_called_class();
$connection = $className::getDb();
switch ($connection->driverName)
{
case 'mysql':
return ConnectionHelper::callMysqlFunction($connection, $mysqlFunction, $params);
break;
default:
break;
}
return FALSE;
}
/**
* 执行存储过程
*
* @return 执行结果
*/
public static function callProcedure($mysqlProcedure, array $params=array())
{
$className = get_called_class();
$connection = $className::getDb();
switch ($connection->driverName)
{
case 'mysql':
return ConnectionHelper::callMysqlProcedure($className::getDb(), $mysqlProcedure, $params);
break;
default:
break;
}
return FALSE;
}
/**
* 获取字段rawName加表别名前缀,主要联表时候防止where条件中字段冲突用的
* @param string $columnName
* @return string
*/
public function getColumnRawName($columnName)
{
$prefix = $this->getTableAlias(true) . '.';
$columns = $this->tableSchema->columns;
if (isset($columns[$columnName]))
{
return $prefix.$columns[$columnName]->rawName;
}
else
{
return $columnName;
}
}
/**
*
* @param mixed $criteria
*/
public function queryAll($criteria = NULL)
{
if ( ! empty($criteria))
{
$this->getDbCriteria()->mergeWith($criteria);
}
$result = $this->getCommandBuilder()
->createFindCommand($this->tableSchema, $this->getDbCriteria())
->queryAll();
$this->setDbCriteria(NULL);
return $result;
}
public function queryRow($criteria = NULL)
{
if ($criteria != NULL)
{
$this->getDbCriteria()->mergeWith($criteria);
}
$result = $this->getCommandBuilder()
->createFindCommand($this->tableSchema, $this->getDbCriteria())
->queryRow();
$this->setDbCriteria(NULL);
return $result;
}
public function compare($column, $value, $partialMatch = FALSE, $operator = 'AND')
{
$criteria = new \CDbCriteria;
$column = $this->getColumnRawName($column);
if ($value === array())
{
$criteria->condition = "1 = 0";
}
else if ($value === '')
{
$criteria->condition = $column." = ''";
}
else
{
$criteria->compare($column, $value, $partialMatch, $operator, TRUE);
}
$this->getDbCriteria()->mergeWith($criteria);
return $this;
}
}
相关文章推荐
- show traffic-shape queue,buffer WFQ队列中与IPP对应的权重值
- iOS:UITextFiled控件的设置
- JS querySelector
- UISnapBehavior-动画效果:迅猛移动弹跳摆动
- iOS UIImageview实现简单动画
- Uevent 上报event事件给上层的详细讲解
- AS3中使用URLRequest发送POST请求
- The attribute value is undefined for the annotation type WebServlet解决方案
- UIActionSheet buttonTitleAtIndex 方法导致程序崩溃的问题
- UIAttachmentBehavior-动画效果:依附,关联
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- iPhone开发之自定义UIActionSheet
- iOS拍照 相册选取 保存PNG图片到Documents下,并利用UICollectionView显示出来
- ant yui 对css,js进行压缩
- UISlider点击轴改变滑块位置
- UIActivityIndicatorView的两种形式
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- [UIImage imageNamed:@""]使用误区
- Android中startActivity的permission检测与UID机制
- ios开发--UIImage转化成NSData