Permalink
Browse files

Adding support for Model.* syntax, which translates to a list of fiel…

…ds from Model.

Fixing buildColumn for null values.
Fixing small containable merge, it was duplicating values, tests added.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8265 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent 9e1dec3 commit 76e50ef625996da0a593c3671db437b47e6b3579 @renan renan committed Jul 31, 2009
@@ -48,7 +48,7 @@ class ContainableBehavior extends ModelBehavior {
var $runtime = array();
/**
* Initiate behavior for the model using specified settings.
- *
+ *
* Available settings:
*
* - recursive: (boolean, optional) set to true to allow containable to automatically
@@ -313,7 +313,7 @@ function containments(&$Model, $contain, $containments = array(), $throwErrors =
$option = 'conditions';
$val = $Model->{$name}->alias.'.'.$key;
}
- $children[$option] = isset($children[$option]) ? array_merge((array) $children[$option], (array) $val) : $val;
+ $children[$option] = is_array($val) ? $val : array($val);
$newChildren = null;
if (!empty($name) && !empty($children[$key])) {
$newChildren = $children[$key];
@@ -296,7 +296,8 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
$fields = parent::fields($model, $alias, $fields, false);
$count = count($fields);
- if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
+ if ($count >= 1 && strpos($fields[0], 'COUNT(*)') === false) {
+ $result = array();
for ($i = 0; $i < $count; $i++) {
$prepend = '';
@@ -307,6 +308,19 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
$fieldAlias = count($this->__fieldMappings);
if (!preg_match('/\s+AS\s+/i', $fields[$i])) {
+ if (substr($fields[$i], -1) == '*') {
+ if (strpos($fields[$i], '.') !== false && $fields[$i] != $alias . '.*') {
+ $build = explode('.', $fields[$i]);
+ $AssociatedModel = $model->{$build[0]};
+ } else {
+ $AssociatedModel = $model;
+ }
+
+ $_fields = $this->fields($AssociatedModel, $AssociatedModel->alias, array_keys($AssociatedModel->schema()));
+ $result = array_merge($result, $_fields);
+ continue;
+ }
+
if (strpos($fields[$i], '.') === false) {
$this->__fieldMappings[$alias . '__' . $fieldAlias] = $alias . '.' . $fields[$i];
$fieldName = $this->name($alias . '.' . $fields[$i]);
@@ -322,10 +336,12 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
}
$fields[$i] = "{$fieldName} AS {$fieldAlias}";
}
- $fields[$i] = $prepend . $fields[$i];
+ $result[] = $prepend . $fields[$i];
}
+ return $result;
+ } else {
+ return $fields;
}
- return $fields;
}
/**
* Generates and executes an SQL INSERT statement for given model, fields, and values.
@@ -374,6 +390,9 @@ function update(&$model, $fields = array(), $values = null, $conditions = null)
if (isset($fields[$model->primaryKey])) {
unset($fields[$model->primaryKey]);
}
+ if (empty($fields)) {
+ return true;
+ }
return parent::update($model, array_keys($fields), array_values($fields), $conditions);
}
/**
@@ -461,8 +480,8 @@ function column($real) {
}
return $col;
}
- $col = str_replace(')', '', $real);
- $limit = null;
+ $col = str_replace(')', '', $real);
+ $limit = null;
if (strpos($col, '(') !== false) {
list($col, $limit) = explode('(', $col);
}
@@ -664,11 +683,13 @@ function insertMulti($table, $fields, $values) {
* @return string
*/
function buildColumn($column) {
- $column = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
- if (strpos($column, 'DEFAULT NULL') !== null) {
- $column = str_replace('DEFAULT NULL', 'NULL', $column);
+ $result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
+ if (strpos($result, 'DEFAULT NULL') !== false) {
+ $result = str_replace('DEFAULT NULL', 'NULL', $result);
+ } else if (array_keys($column) == array('type', 'name')) {
+ $result .= ' NULL';
}
- return $column;
+ return $result;
}
/**
* Format indexes for create table
@@ -1405,6 +1405,7 @@ function update(&$model, $fields = array(), $values = null, $conditions = null)
function _prepareUpdateFields(&$model, $fields, $quoteValues = true, $alias = false) {
$quotedAlias = $this->startQuote . $model->alias . $this->endQuote;
+ $updates = array();
foreach ($fields as $field => $value) {
if ($alias && strpos($field, '.') === false) {
$quoted = $model->escapeField($field);
@@ -1982,70 +1982,76 @@ function testSettingsThirdLevel() {
);
$this->assertEqual($result, $expected);
- $result = $this->User->find('all', array('contain' => array(
- 'ArticleFeatured' => array(
- 'title', 'order' => 'title DESC',
- 'Featured' => array(
- 'category_id',
- 'Category' => 'name'
- )
- )
- )));
- $expected = array(
- array(
- 'User' => array(
- 'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
- ),
+ $orders = array(
+ 'title DESC', 'title DESC, published DESC',
+ array('title' => 'DESC'), array('title' => 'DESC', 'published' => 'DESC'),
+ );
+ foreach ($orders as $order) {
+ $result = $this->User->find('all', array('contain' => array(
'ArticleFeatured' => array(
- array(
- 'title' => 'Third Article', 'id' => 3, 'user_id' => 1,
- 'Featured' => array()
+ 'title', 'order' => $order,
+ 'Featured' => array(
+ 'category_id',
+ 'Category' => 'name'
+ )
+ )
+ )));
+ $expected = array(
+ array(
+ 'User' => array(
+ 'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
+ 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
),
- array(
- 'title' => 'First Article', 'id' => 1, 'user_id' => 1,
- 'Featured' => array(
- 'category_id' => 1, 'id' => 1,
- 'Category' => array(
- 'name' => 'Category 1'
+ 'ArticleFeatured' => array(
+ array(
+ 'title' => 'Third Article', 'id' => 3, 'user_id' => 1,
+ 'Featured' => array()
+ ),
+ array(
+ 'title' => 'First Article', 'id' => 1, 'user_id' => 1,
+ 'Featured' => array(
+ 'category_id' => 1, 'id' => 1,
+ 'Category' => array(
+ 'name' => 'Category 1'
+ )
)
)
)
- )
- ),
- array(
- 'User' => array(
- 'id' => 2, 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'
),
- 'ArticleFeatured' => array()
- ),
- array(
- 'User' => array(
- 'id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'
+ array(
+ 'User' => array(
+ 'id' => 2, 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
+ 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'
+ ),
+ 'ArticleFeatured' => array()
),
- 'ArticleFeatured' => array(
- array(
- 'title' => 'Second Article', 'id' => 2, 'user_id' => 3,
- 'Featured' => array(
- 'category_id' => 1, 'id' => 2,
- 'Category' => array(
- 'name' => 'Category 1'
+ array(
+ 'User' => array(
+ 'id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
+ 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'
+ ),
+ 'ArticleFeatured' => array(
+ array(
+ 'title' => 'Second Article', 'id' => 2, 'user_id' => 3,
+ 'Featured' => array(
+ 'category_id' => 1, 'id' => 2,
+ 'Category' => array(
+ 'name' => 'Category 1'
+ )
)
)
)
- )
- ),
- array(
- 'User' => array(
- 'id' => 4, 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
- 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'
),
- 'ArticleFeatured' => array()
- )
- );
- $this->assertEqual($result, $expected);
+ array(
+ 'User' => array(
+ 'id' => 4, 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
+ 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'
+ ),
+ 'ArticleFeatured' => array()
+ )
+ );
+ $this->assertEqual($result, $expected);
+ }
}
/**
* testFindThirdLevelNonReset method
@@ -3225,7 +3231,6 @@ function testOriginalAssociations() {
$options = array(
'conditions' => array(
- 'Comment.comment !=' => 'Crazy',
'Comment.published' => 'Y',
),
'contain' => 'User',
@@ -3236,7 +3241,6 @@ function testOriginalAssociations() {
$dummyResult = $this->Article->Comment->find('all', array(
'conditions' => array(
- 'Comment.comment !=' => 'Silly',
'User.user' => 'mariano'
),
'fields' => array('User.password'),
@@ -3320,7 +3324,6 @@ function testResetAssociation() {
$initialOptions = array(
'conditions' => array(
- 'Comment.comment' => '!= Crazy',
'Comment.published' => 'Y',
),
'contain' => 'User',
@@ -3331,7 +3334,6 @@ function testResetAssociation() {
$findOptions = array(
'conditions' => array(
- 'Comment.comment !=' => 'Silly',
'User.user' => 'mariano',
),
'fields' => array('User.password'),
@@ -3409,7 +3411,8 @@ function testResetMultipleHabtmAssociations() {
'joinTable' => 'articles_tags',
'foreignKey' => 'article_id',
'associationForeignKey' => 'tag_id',
- 'conditions' => 'LENGTH(ShortTag.tag) <= 3'
+ // LENGHT function mysql-only, using LIKE does almost the same
+ 'conditions' => 'ShortTag.tag LIKE "???"'
)
)
);
@@ -427,15 +427,18 @@ function testTranslatedFindList() {
$expected = array(1 => 'Titel #1', 2 => 'Titel #2', 3 => 'Titel #3');
$this->assertEqual($result, $expected);
- $debug = Configure::read('debug');
- Configure::write('debug', 0);
-
- $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => false));
- $this->assertEqual($result, array());
-
- $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'after'));
- $this->assertEqual($result, array());
- Configure::write('debug', $debug);
+ // MSSQL trigger an error and stops the page even if the debug = 0
+ if ($this->db->config['driver'] != 'mssql') {
+ $debug = Configure::read('debug');
+ Configure::write('debug', 0);
+
+ $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => false));
+ $this->assertEqual($result, array());
+
+ $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'after'));
+ $this->assertEqual($result, array());
+ Configure::write('debug', $debug);
+ }
$result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'before'));
$expected = array(1 => null, 2 => null, 3 => null);
Oops, something went wrong. Retry.

0 comments on commit 76e50ef

Please sign in to comment.