Skip to content

Commit

Permalink
Merge pull request #130 from cakephp/fix-pgsql
Browse files Browse the repository at this point in the history
Fix query generation for postgresql
  • Loading branch information
dakota authored Nov 20, 2017
2 parents 0e7265b + 14869e5 commit cc8b999
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ services:

env:
matrix:
- DB=mysql db_dsn='mysql://root@0.0.0.0/cakephp_test'
- DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'
- DB=sqlite db_dsn='sqlite:///:memory:'
- DB=mysql db_dsn='mysql://root@0.0.0.0/cakephp_test' quoteIdentifiers=false
- DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test' quoteIdentifiers=true
- DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test' quoteIdentifiers=false
- DB=sqlite db_dsn='sqlite:///:memory:' quoteIdentifiers=false
global:
- DEFAULT=1
- MINIMUMS=0
Expand Down
30 changes: 16 additions & 14 deletions src/Model/Table/AclNodesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

use Cake\Core\Configure;
use Cake\Core\Exception;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Entity;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;

Expand All @@ -40,16 +42,16 @@ public static function defaultConnectionName()
/**
* Retrieves the Aro/Aco node for this model
*
* @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
* @return array Node found in database
* @param string|array|Table $ref Array with 'model' and 'foreign_key', model object, or string value
* @return array|Query|false Node found in database
* @throws \Cake\Core\Exception\Exception when binding to a model that doesn't exist.
*/
public function node($ref = null)
{
$db = $this->connection();
$type = $this->alias();
$table = $this->table();
$result = null;
$query = false;

if (empty($ref)) {
return null;
Expand All @@ -64,8 +66,8 @@ public function node($ref = null)

$queryData = [
'conditions' => [
"{$type}.lft" . ' <= ' . "{$type}0.lft",
"{$type}.rght" . ' >= ' . "{$type}0.rght",
"{$type}.lft" . ' <= ' => new IdentifierExpression("{$type}0.lft"),
"{$type}.rght" . ' >= ' => new IdentifierExpression("{$type}0.rght"),
],
'fields' => ['id', 'parent_id', 'model', 'foreign_key', 'alias'],
'join' => [[
Expand All @@ -74,7 +76,7 @@ public function node($ref = null)
'type' => 'INNER',
'conditions' => ["{$type}0.alias" => $start]
]],
'order' => "{$type}.lft" . ' DESC'
'order' => ["{$type}.lft" => 'DESC']
];

foreach ($path as $i => $alias) {
Expand All @@ -85,17 +87,17 @@ public function node($ref = null)
'alias' => "{$type}{$i}",
'type' => 'INNER',
'conditions' => [
"{$type}{$i}.lft" . ' > ' . "{$type}{$j}.lft",
"{$type}{$i}.rght" . ' < ' . "{$type}{$j}.rght",
"{$type}{$i}.lft" . ' > ' => new IdentifierExpression("{$type}{$j}.lft"),
"{$type}{$i}.rght" . ' < ' => new IdentifierExpression("{$type}{$j}.rght"),
"{$type}{$i}.alias" => $alias,
"{$type}{$j}.id" . ' = ' . "{$type}{$i}.parent_id"
"{$type}{$j}.id" . ' = ' => new IdentifierExpression("{$type}{$i}.parent_id")
]
];

$queryData['conditions'] = [
'or' => [
"{$type}.lft" . ' <= ' . "{$type}0.lft" . ' AND ' . "{$type}.rght" . ' >= ' . "{$type}0.rght",
"{$type}.lft" . ' <= ' . "{$type}{$i}.lft" . ' AND ' . "{$type}.rght" . ' >= ' . "{$type}{$i}.rght"
["{$type}.lft" . ' <= ' => new IdentifierExpression("{$type}0.lft"), "{$type}.rght" . ' >= ' => new IdentifierExpression("{$type}0.rght")],
["{$type}.lft" . ' <= ' => new IdentifierExpression("{$type}{$i}.lft"), "{$type}.rght" . ' >= ' => new IdentifierExpression("{$type}{$i}.rght")]
]
];
}
Expand Down Expand Up @@ -170,12 +172,12 @@ public function node($ref = null)
'alias' => "{$type}0",
'type' => 'INNER',
'conditions' => [
"{$type}.lft" . ' <= ' . "{$type}0.lft",
"{$type}.rght" . ' >= ' . "{$type}0.rght",
"{$type}.lft" . ' <= ' => new IdentifierExpression("{$type}0.lft"),
"{$type}.rght" . ' >= ' => new IdentifierExpression("{$type}0.rght"),
]
]
],
'order' => "{$type}.lft" . ' DESC',
'order' => ["{$type}.lft" => 'DESC'],
];
$query = $this->find('all', $queryData);

Expand Down
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
'database' => getenv('db_database'),
'username' => getenv('db_login'),
'password' => getenv('db_password'),
'timezone' => 'UTC'
'timezone' => 'UTC',
'quoteIdentifiers' => getenv('quoteIdentifiers'),
]);

Configure::write('Session', [
Expand Down

0 comments on commit cc8b999

Please sign in to comment.