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 > 1 && $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($model, null, $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, $null, null, null, null, $queryData, false, $null);
}
}
?>