dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | flex | CRM | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>PHP>php技术资料>文章内容
cakephp用sql函数扩展DboSource和Model
来源: 作者:
有些时候,SQL语句对于查询来说是很有用并且和有效的,但是cakephp没有提供手动的查询。
看这个例子。我们已经有一个用户的表,所以我们想要选择用户在哪个组里面。


Download code
select * from users
where group_id in (select group_id from users where id=$u1).


如何让cakephp查询

这是最大可能的方式
Download code
$this->User->findAll(array('group_id' => "in (select group_id from users where id=$u1)"));


下面的例子有所改动

Download code
$this->User->findAll(array('group_id' => "in -!(". $this->User->getQuery(array('id'=> $u1) . ')', 'group_id')));


Function Model::getQuery shuld take similar parameter as Model::findAll method and return SQL query to DB.

Is this possible to create such function without changing CORE files?
My answer is YES!!!
Really all what we need is extending of Model and DboMysql classes.
We need to add two methods - both are based on cake core libraries (Model::findAll and DboSource::read).


For Model class all pretty simple. We just place our new function to AppModel or to some behavior.
But what about extending DboMysql driver class? Is this possible. Really cakephp 1.2 support such feature.
We need to create class extension in folder app/model/datasource/dbo
I name it DboMysqlEx – file is dbo_mysql_ex.php.


配置:

在app/config/database.php 我们需要用一个driver: 'driver' => 'mysql_ex',

DATABASE_CONFIG


Download code

class DATABASE_CONFIG {


    var $default = array(
        'driver' => 'mysql_ex',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root',
        'password' => '',
        'database' => 'testing',
        'prefix' => ''
    );
}



APP_MODEL:



Model Class:

Download code <?php 
class AppModel extends Model
{
    function 
getQuery($conditions null$fields null$order null$limit null$page 1$recursive null
) {
        
$db =& ConnectionManager::getDataSource($this->useDbConfig
);
        
$this->id $this->getID
();
        
$offset null
;

        if (empty(
$page) || !is_numeric($page) || intval($page) < 1
) {
            
$page 1
;
        }

        if (
$page && $limit != null
) {
            
$offset = ($page 1) * $limit
;
        }

        if (
$order == null && $order !== false
) {
            if (
$this->order == null
) {
                
$order 
= array();
            } else {
                
$order = array($this->order
);
            }
        } else {
            
$order = array($order
);
        }

        
$queryData 
= array(
            
'conditions' => $conditions
,
            
'fields'    => $fields
,
            
'joins'     
=> array(),
            
'limit'     => $limit
,
            
'offset'    => $offset
,
            
'order'     => 
$order
        
);

        if (!empty(
$this->behaviors
)) {
            
$behaviors array_keys($this->behaviors
);
            
$ct count($behaviors
);
            for (
$i 0$i $ct$i
++) {
                
$ret $this->behaviors[$behaviors[$i]]->beforeFind($this$queryData
);
                if (
is_array($ret
)) {
                    
$queryData $ret
;
                } elseif (
$ret === false
) {
                    return 
null
;
                }
            }
        }

        
$ret $this->beforeFind($queryData
);
        if (
is_array($ret
)) {
            
$queryData $ret
;
        } elseif (
$ret === false
) {
            return 
null
;
        }

        return 
$db->queryGet($this$queryData$recursive
);
    }
}
?>



app/model/datasource/dbo/dbo_mysql_ex.php




Model Class:

Download code <?php 
 
require (LIBS 'model' DS 'datasources' DS 'dbo' DS 'dbo_mysql.php'
);
 
class 
DboMysqlEx extends DboMysql 
{
/**
 * Enter description here...
 *
 * @var unknown_type
 */
    
var $description "MySQL DBO Extension Driver"
;
    
    function 
queryGet(&$model$queryData = array(), $recursive null
) {
        
$this->__scrubQueryData($queryData
);
        
$null null
;
        
$array 
= array();
        
$linkedModels 
= array();
        
$this->__bypass false
;

        if (!
is_null($recursive
)) {
            
$_recursive $model->recursive
;
            
$model->recursive $recursive
;
        }

        if (!empty(
$queryData['fields'
])) {
            
$this->__bypass true
;
            
$queryData['fields'] = $this->fields($modelnull$queryData['fields'
]);
        } else {
            
$queryData['fields'] = $this->fields($model
);
        }

        foreach (
$model->__associations as $type
) {
            foreach (
$model->{$type} as $assoc => $assocData
) {
                if (
$model->recursive > -1
) {
                    
$linkModel =& $model->{$assoc
};

                    
$external = isset($assocData['external'
]);
                    if (
$model->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany'
) {
                        if (
true === $this->generateSelfAssociationQuery($model$linkModel$type$assoc$assocData$queryData$external$null
)) {
                            
$linkedModels[] = $type '/' $assoc
;
                        }
                    } else {
                        if (
$model->useDbConfig == $linkModel->useDbConfig
) {
                            if (
true === $this->generateAssociationQuery($model$linkModel$type$assoc$assocData$queryData$external$null
)) {
                                
$linkedModels[] = $type '/' $assoc
;
                            }
                        }
                    }
                }
            }
        }
        
// Build final query SQL
        
return $this->generateAssociationQuery($model$nullnullnullnull$queryDatafalse$null
);
    }
    
    
}
?>

上一篇:安装CakePHP   下一篇:十款PHP开发框架横向比较
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·通过对PHP服务器端特性的配置加
·php与mysql三日通
·AJAX技术在PHP开发中的简单应用
·大家所使用的PHP开发环境
·PHP缓存的实现
·针对PHP新手总结的PHP基础知识
·一个简单实现多条件查询的例子
·PHP串行化变量和序列化对象
·PHP表单
·推荐阅读:php技术生成静态页面
·php实用函数
·php生成随机数
  相关文章
·安装CakePHP
·十款PHP开发框架横向比较
·提高MySQL 查询效率的三个技巧
·IBM开放协同开发Jazz框架核心组
·PHP配置文件PHP.INI的中文注释版
·利用PHP制作简单的内容采集器
·在PHP中全面阻止SQL注入式攻击之
·在PHP中全面阻止SQL注入式攻击之
·在PHP中全面阻止SQL注入式攻击之
·升级PHP5的理由:PHP4和PHP5性能
·用 PHP V5 开发多任务应用程序
·CakePHP你必须知道的21条技巧
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763