让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;
    }
    
    
}

让ar执行queryall和queryrow方法返回数组的更多相关文章

  1. 083、Java数组之方法返回数组

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  2. JAVAEE——SpringMVC第二天:高级参数绑定、@RequestMapping、方法返回值、异常处理、图片上传、Json交互、实现RESTful、拦截器

    1. 课前回顾 https://www.cnblogs.com/xieyupeng/p/9093661.html 2. 课程计划 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 ...

  3. JS 数组方法 array数组声明 元素的添加和删除 等

    声明数组 var arr1 = [1,2,3,4,5]; var arr2 = new Array(100); //声明长度为100的arr2数组. arr2=[]; arr2.length = 10 ...

  4. C#线程池ThreadPool.QueueUserWorkItem接收线程执行的方法返回值

    最近在项目中需要用到多线程,考虑了一番,选择了ThreadPool,我的需求是要拿到线程执行方法的返回值, 但是ThreadPool.QueueUserWorkItem的回调方法默认是没有返回值的,搜 ...

  5. 在IE浏览器中执行OpenFlashChart的reload方法时无法刷新的解决方法

    由于项目需求,需要在网页上利用图表展示相关数据的统计信息,采用了OpenFlashChart技术.OpenFlashChart是一款开源的以Flash和Javascript为技术基础的免费图表,用它能 ...

  6. EFCore执行Sql语句的方法:FromSql与ExecuteSqlCommand

    前言 在EFCore中执行Sql语句的方法为:FromSql与ExecuteSqlCommand:在EF6中的为SqlQuery与ExecuteSqlCommand,而FromSql和SqlQuery ...

  7. 通过反射执行get、set方法

    Class clazz = sourceObj.getClass(); 1.获取所有属性 BeanInfo beanInfo = Introspector.getBeanInfo(clazz); Pr ...

  8. 如何循环遍历document.querySelectorAll()方法返回的结果

    使用JavaScript的forEach方法,我们可以轻松的循环一个数组,但如果你认为document.querySelectorAll()方法返回的应该是个数组,而使用forEach循环它: /* ...

  9. 是否需要手动执行DataContext的Dispose方法?

    我们知道DataContext实现了IDisposable接口.在C#中,凡是实现了IDisposable接口的类,都推荐的使用using语句.如下: using (DataContext db = ...

随机推荐

  1. tc 146 2 RectangularGrid(数学推导)

    SRM 146 2 500RectangularGrid Problem Statement Given the width and height of a rectangular grid, ret ...

  2. js兼容注意事项--仅供参考

    做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...

  3. Memcached和Redis对比和适用场景

    关于memcached和redis的使用场景,根据大神们的讨论和我在网上查到的资料,总结一下: 两者对比: redis提供数据持久化功能,memcached无持久化: redis的数据结构比memca ...

  4. 4 个最好的 Linux 引导程序

    导读 当你打开你的机器,开机自检(POST)成功完成后,BIOS(基本输入输出系统)立即定位所配置的引导介质,并从 MBR(主引导记录)或 GUID(全局唯一标识符)分区表读取一些命令,这是引导介质的 ...

  5. Lucene4.1 视频学习

    1.将路径建立在内存中 Directory d = new RAMDirectiry(); 2.Filed Index(索引选项):Index.ANALYZED:进行分词和索引,适应于标题,内容等In ...

  6. 全部springxml文件约束 applicationContext.xml

    <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.sp ...

  7. Google java style

    这里主要是记录关于java style的笔记. 1,Google的. 博客链接(中文):http://www.cnblogs.com/lanxuezaipiao/p/3534447.html. 官方链 ...

  8. Linux--网络通信命令(给其它用户发送广播消息)

    1.命令名称:write 执行权限:所有用户  功能描述:向另外一个用户发送信息,以CTRL+D作为结束 语法:write <用户名>root向luxh用户发送信息[root@localh ...

  9. Git索引

    原文: http://gitbook.liuhui998.com/7_4.html git中的索引(index)是一个存放了排好序的路径的二进制文件(通常是.git/index), 每一个条目都附带有 ...

  10. Unity3d 制作物品平滑运动

    直接贴代码了 using UnityEngine; using System; using System.Collections; using System; using DataTable; pub ...