Skip to content

Commit

Permalink
Use interface const instead of magic strings for LEFT/RIGHT/INNER joi…
Browse files Browse the repository at this point in the history
…n types.
  • Loading branch information
dereuromark committed Sep 25, 2017
1 parent 8dea5d8 commit d5ae641
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/Database/Query.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Database\Expression\QueryExpression; use Cake\Database\Expression\QueryExpression;
use Cake\Database\Expression\ValuesExpression; use Cake\Database\Expression\ValuesExpression;
use Cake\Database\Statement\CallbackStatement; use Cake\Database\Statement\CallbackStatement;
use Cake\Datasource\QueryInterface;
use InvalidArgumentException; use InvalidArgumentException;
use IteratorAggregate; use IteratorAggregate;
use RuntimeException; use RuntimeException;
Expand Down Expand Up @@ -613,7 +614,7 @@ public function join($tables = null, $types = [], $overwrite = false)
$t['conditions'] = $this->newExpr()->add($t['conditions'], $types); $t['conditions'] = $this->newExpr()->add($t['conditions'], $types);
} }
$alias = is_string($alias) ? $alias : null; $alias = is_string($alias) ? $alias : null;
$joins[$alias ?: $i++] = $t + ['type' => 'INNER', 'alias' => $alias]; $joins[$alias ?: $i++] = $t + ['type' => QueryInterface::JOIN_TYPE_INNER, 'alias' => $alias];
} }


if ($overwrite) { if ($overwrite) {
Expand Down Expand Up @@ -683,7 +684,7 @@ public function removeJoin($name)
*/ */
public function leftJoin($table, $conditions = [], $types = []) public function leftJoin($table, $conditions = [], $types = [])
{ {
return $this->join($this->_makeJoin($table, $conditions, 'LEFT'), $types); return $this->join($this->_makeJoin($table, $conditions, QueryInterface::JOIN_TYPE_LEFT), $types);
} }


/** /**
Expand All @@ -703,7 +704,7 @@ public function leftJoin($table, $conditions = [], $types = [])
*/ */
public function rightJoin($table, $conditions = [], $types = []) public function rightJoin($table, $conditions = [], $types = [])
{ {
return $this->join($this->_makeJoin($table, $conditions, 'RIGHT'), $types); return $this->join($this->_makeJoin($table, $conditions, QueryInterface::JOIN_TYPE_RIGHT), $types);
} }


/** /**
Expand All @@ -723,7 +724,7 @@ public function rightJoin($table, $conditions = [], $types = [])
*/ */
public function innerJoin($table, $conditions = [], $types = []) public function innerJoin($table, $conditions = [], $types = [])
{ {
return $this->join($this->_makeJoin($table, $conditions, 'INNER'), $types); return $this->join($this->_makeJoin($table, $conditions, QueryInterface::JOIN_TYPE_INNER), $types);
} }


/** /**
Expand Down
4 changes: 4 additions & 0 deletions src/Datasource/QueryInterface.php
Expand Up @@ -23,6 +23,10 @@
interface QueryInterface interface QueryInterface
{ {


const JOIN_TYPE_INNER = 'INNER';
const JOIN_TYPE_LEFT = 'LEFT';
const JOIN_TYPE_RIGHT = 'RIGHT';

/** /**
* Returns a key => value array representing a single aliased field * Returns a key => value array representing a single aliased field
* that can be passed directly to the select() method. * that can be passed directly to the select() method.
Expand Down
3 changes: 2 additions & 1 deletion src/ORM/Association.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Core\ConventionsTrait; use Cake\Core\ConventionsTrait;
use Cake\Database\Expression\IdentifierExpression; use Cake\Database\Expression\IdentifierExpression;
use Cake\Datasource\EntityInterface; use Cake\Datasource\EntityInterface;
use Cake\Datasource\QueryInterface;
use Cake\Datasource\ResultSetDecorator; use Cake\Datasource\ResultSetDecorator;
use Cake\ORM\Locator\LocatorAwareTrait; use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
Expand Down Expand Up @@ -158,7 +159,7 @@ abstract class Association
* *
* @var string * @var string
*/ */
protected $_joinType = 'LEFT'; protected $_joinType = QueryInterface::JOIN_TYPE_LEFT;


/** /**
* The property name that should be filled with data from the target table * The property name that should be filled with data from the target table
Expand Down
5 changes: 3 additions & 2 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Database\ExpressionInterface; use Cake\Database\ExpressionInterface;
use Cake\Database\Expression\IdentifierExpression; use Cake\Database\Expression\IdentifierExpression;
use Cake\Datasource\EntityInterface; use Cake\Datasource\EntityInterface;
use Cake\Datasource\QueryInterface;
use Cake\ORM\Association; use Cake\ORM\Association;
use Cake\ORM\Association\Loader\SelectWithPivotLoader; use Cake\ORM\Association\Loader\SelectWithPivotLoader;
use Cake\ORM\Query; use Cake\ORM\Query;
Expand Down Expand Up @@ -55,7 +56,7 @@ class BelongsToMany extends Association
* *
* @var string * @var string
*/ */
protected $_joinType = 'INNER'; protected $_joinType = QueryInterface::JOIN_TYPE_INNER;


/** /**
* The strategy name to be used to fetch associated records. * The strategy name to be used to fetch associated records.
Expand Down Expand Up @@ -1120,7 +1121,7 @@ protected function _appendJunctionJoin($query, $conditions)
$name => [ $name => [
'table' => $this->junction()->getTable(), 'table' => $this->junction()->getTable(),
'conditions' => $conditions, 'conditions' => $conditions,
'type' => 'INNER' 'type' => QueryInterface::JOIN_TYPE_INNER
] ]
]; ];


Expand Down
3 changes: 2 additions & 1 deletion src/ORM/Association/HasMany.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Database\Expression\FieldInterface; use Cake\Database\Expression\FieldInterface;
use Cake\Database\Expression\QueryExpression; use Cake\Database\Expression\QueryExpression;
use Cake\Datasource\EntityInterface; use Cake\Datasource\EntityInterface;
use Cake\Datasource\QueryInterface;
use Cake\ORM\Association; use Cake\ORM\Association;
use Cake\ORM\Association\DependentDeleteHelper; use Cake\ORM\Association\DependentDeleteHelper;
use Cake\ORM\Association\Loader\SelectLoader; use Cake\ORM\Association\Loader\SelectLoader;
Expand Down Expand Up @@ -47,7 +48,7 @@ class HasMany extends Association
* *
* @var string * @var string
*/ */
protected $_joinType = 'INNER'; protected $_joinType = QueryInterface::JOIN_TYPE_INNER;


/** /**
* The strategy name to be used to fetch associated records. * The strategy name to be used to fetch associated records.
Expand Down
5 changes: 3 additions & 2 deletions src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -17,6 +17,7 @@
use ArrayObject; use ArrayObject;
use Cake\Collection\Collection; use Cake\Collection\Collection;
use Cake\Datasource\EntityInterface; use Cake\Datasource\EntityInterface;
use Cake\Datasource\QueryInterface;
use Cake\Event\Event; use Cake\Event\Event;
use Cake\I18n\I18n; use Cake\I18n\I18n;
use Cake\ORM\Behavior; use Cake\ORM\Behavior;
Expand Down Expand Up @@ -173,7 +174,7 @@ public function setupFieldAssociations($fields, $table, $model, $strategy)
$this->_table->hasOne($name, [ $this->_table->hasOne($name, [
'targetTable' => $fieldTable, 'targetTable' => $fieldTable,
'foreignKey' => 'foreign_key', 'foreignKey' => 'foreign_key',
'joinType' => $filter ? 'INNER' : 'LEFT', 'joinType' => $filter ? QueryInterface::JOIN_TYPE_INNER : QueryInterface::JOIN_TYPE_LEFT,
'conditions' => $conditions, 'conditions' => $conditions,
'propertyName' => $field . '_translation' 'propertyName' => $field . '_translation'
]); ]);
Expand Down Expand Up @@ -246,7 +247,7 @@ public function beforeFind(Event $event, Query $query, $options)
); );


if ($changeFilter) { if ($changeFilter) {
$filter = $options['filterByCurrentLocale'] ? 'INNER' : 'LEFT'; $filter = $options['filterByCurrentLocale'] ? QueryInterface::JOIN_TYPE_INNER : QueryInterface::JOIN_TYPE_LEFT;
$contain[$name]['joinType'] = $filter; $contain[$name]['joinType'] = $filter;
} }
} }
Expand Down
3 changes: 2 additions & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -16,6 +16,7 @@


use Cake\Database\Statement\BufferedStatement; use Cake\Database\Statement\BufferedStatement;
use Cake\Database\Statement\CallbackStatement; use Cake\Database\Statement\CallbackStatement;
use Cake\Datasource\QueryInterface;
use Closure; use Closure;
use InvalidArgumentException; use InvalidArgumentException;


Expand Down Expand Up @@ -238,7 +239,7 @@ public function setMatching($assoc, callable $builder = null, $options = [])
} }


if (!isset($options['joinType'])) { if (!isset($options['joinType'])) {
$options['joinType'] = 'INNER'; $options['joinType'] = QueryInterface::JOIN_TYPE_INNER;
} }


$assocs = explode('.', $assoc); $assocs = explode('.', $assoc);
Expand Down
6 changes: 3 additions & 3 deletions src/ORM/Query.php
Expand Up @@ -573,7 +573,7 @@ public function leftJoinWith($assoc, callable $builder = null)
{ {
$result = $this->getEagerLoader() $result = $this->getEagerLoader()
->setMatching($assoc, $builder, [ ->setMatching($assoc, $builder, [
'joinType' => 'LEFT', 'joinType' => QueryInterface::JOIN_TYPE_LEFT,
'fields' => false 'fields' => false
]) ])
->getMatching(); ->getMatching();
Expand Down Expand Up @@ -622,7 +622,7 @@ public function innerJoinWith($assoc, callable $builder = null)
{ {
$result = $this->getEagerLoader() $result = $this->getEagerLoader()
->setMatching($assoc, $builder, [ ->setMatching($assoc, $builder, [
'joinType' => 'INNER', 'joinType' => QueryInterface::JOIN_TYPE_INNER,
'fields' => false 'fields' => false
]) ])
->getMatching(); ->getMatching();
Expand Down Expand Up @@ -686,7 +686,7 @@ public function notMatching($assoc, callable $builder = null)
{ {
$result = $this->getEagerLoader() $result = $this->getEagerLoader()
->setMatching($assoc, $builder, [ ->setMatching($assoc, $builder, [
'joinType' => 'LEFT', 'joinType' => QueryInterface::JOIN_TYPE_LEFT,
'fields' => false, 'fields' => false,
'negateMatch' => true 'negateMatch' => true
]) ])
Expand Down

0 comments on commit d5ae641

Please sign in to comment.