Skip to content

Commit

Permalink
Adding a method to mark a query as dirty, using it to add extra logic
Browse files Browse the repository at this point in the history
when a query internal state is altered
  • Loading branch information
lorenzo committed Jul 2, 2013
1 parent 7b19512 commit 8f82ff5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
35 changes: 23 additions & 12 deletions lib/Cake/Database/Query.php
Expand Up @@ -160,7 +160,7 @@ public function connection($connection = null) {
if ($connection === null) {
return $this->_connection;
}
$this->_dirty = true;
$this->_dirty();
$this->_connection = $connection;
return $this;
}
Expand Down Expand Up @@ -352,7 +352,7 @@ public function select($fields = [], $overwrite = false) {
$this->_parts['select'] = array_merge($this->_parts['select'], $fields);
}

$this->_dirty = true;
$this->_dirty();
$this->_type = 'select';
return $this;
}
Expand Down Expand Up @@ -396,7 +396,7 @@ public function distinct($on = [], $overwrite = false) {
}

$this->_parts['distinct'] = $on;
$this->_dirty = true;
$this->_dirty();
return $this;
}

Expand Down Expand Up @@ -474,7 +474,7 @@ public function from($tables = [], $overwrite = false) {
$this->_parts['from'] = array_merge($this->_parts['from'], $tables);
}

$this->_dirty = true;
$this->_dirty();
return $this;
}

Expand Down Expand Up @@ -605,7 +605,7 @@ public function join($tables = null, $types = [], $overwrite = false) {
$this->_parts['join'] = array_merge($this->_parts['join'], array_values($joins));
}

$this->_dirty = true;
$this->_dirty();
return $this;
}

Expand Down Expand Up @@ -974,7 +974,7 @@ public function group($fields, $overwrite = false) {
}

$this->_parts['group'] = array_merge($this->_parts['group'], array_values($fields));
$this->_dirty = true;
$this->_dirty();
return $this;
}

Expand Down Expand Up @@ -1118,7 +1118,7 @@ public function union($query, $all = false, $overwrite = false) {
$this->_parts['union'] = [];
}
$this->_parts['union'][] = compact('all', 'query');
$this->_dirty = true;
$this->_dirty();
return $this;
}

Expand Down Expand Up @@ -1172,7 +1172,7 @@ protected function _buildValuesPart($parts) {
* @return Query
*/
public function insert($table, $columns, $types = []) {
$this->_dirty = true;
$this->_dirty();
$this->_type = 'insert';
$this->_parts['insert'] = [$table, $columns];
$this->_parts['values'] = new ValuesExpression($columns, $types + $this->defaultTypes());
Expand Down Expand Up @@ -1203,7 +1203,7 @@ public function values($data) {
);
}

$this->_dirty = true;
$this->_dirty();
if ($data instanceof ValuesExpression) {
$this->_parts['values'] = $data;
return $this;
Expand All @@ -1222,7 +1222,7 @@ public function values($data) {
* @return Query
*/
public function update($table) {
$this->_dirty = true;
$this->_dirty();
$this->_type = 'update';
$this->_parts['update'][0] = $table;
return $this;
Expand Down Expand Up @@ -1264,7 +1264,7 @@ public function set($key, $value = null, $types = []) {
* @return Query
*/
public function delete($table = null) {
$this->_dirty = true;
$this->_dirty();
$this->_type = 'delete';
if ($table) {
$this->from($table);
Expand Down Expand Up @@ -1491,7 +1491,7 @@ protected function _conjugate($part, $append, $conjunction, $types) {
}

$this->_parts[$part] = $expression;
$this->_dirty = true;
$this->_dirty();
}

/**
Expand Down Expand Up @@ -1540,6 +1540,17 @@ protected function _transformQuery() {
return $transformed;
}

/**
* Marks a query as dirty, removing any preprocessed information
* from in memory caching
*
* @return void
*/
protected function _dirty() {
$this->_dirty = true;
$this->_transformedQuery = null;
}

/**
* Returns string representation of this query (complete SQL statement)
*
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/ORM/Query.php
Expand Up @@ -231,7 +231,7 @@ public function addDefaultTypes(Table $table) {
*/
public function contain($associations = null, $override = false) {
if ($this->_containments === null || $override) {
$this->_dirty = true;
$this->_dirty();
$this->_containments = new \ArrayObject;
}

Expand All @@ -249,7 +249,7 @@ public function contain($associations = null, $override = false) {
$associations = array_merge($old, $this->_reformatContain($associations));
$this->_containments->exchangeArray($associations);
$this->_normalizedContainments = null;
$this->_dirty = true;
$this->_dirty();
return $this;
}

Expand Down

0 comments on commit 8f82ff5

Please sign in to comment.