Permalink
Browse files

Merge remote branch 'origin/1.2' into 1.2-merger

Conflicts:
	cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php
  • Loading branch information...
markstory committed Mar 16, 2010
2 parents cadac9c + 01a5738 commit 4861da66edcad7409f5e1460bdbc5295bcaa699a
@@ -465,6 +465,7 @@ function compare($old, $new = null) {
$tables[$table]['drop'] = $diff;
}
}
+
foreach ($fields as $field => $value) {
if (isset($old[$table][$field])) {
$diff = array_diff_assoc($value, $old[$table][$field]);
@@ -1658,6 +1658,22 @@ function _matchRecords(&$model, $conditions = null) {
} elseif ($conditions === null) {
$conditions = $this->conditions($this->defaultConditions($model, $conditions, false), true, true, $model);
} else {
+ $noJoin = true;
+ foreach ($conditions as $field => $value) {
+ $originalField = $field;
+ if (strpos($field, '.') !== false) {
+ list($alias, $field) = explode('.', $field);
+ }
+ if (!$model->hasField($field)) {
+ $noJoin = false;
+ break;
+ }
+ $conditions[$field] = $value;
+ unset($conditions[$originalField]);
+ }
+ if ($noJoin === true) {
+ return $this->conditions($conditions);
+ }
$idList = $model->find('all', array(
'fields' => "{$model->alias}.{$model->primaryKey}",
'conditions' => $conditions
View
@@ -1429,10 +1429,12 @@ function __saveMulti($joined, $id, &$db) {
}
if ($this->hasAndBelongsToMany[$assoc]['unique']) {
- $conditions = array_merge(
- array($join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id),
- (array)$this->hasAndBelongsToMany[$assoc]['conditions']
+ $conditions = array(
+ $join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id
);
+ if (!empty($this->hasAndBelongsToMany[$assoc]['conditions'])) {
+ $conditions = array_merge($conditions, (array)$this->hasAndBelongsToMany[$assoc]['conditions']);
+ }
$links = $this->{$join}->find('all', array(
'conditions' => $conditions,
'recursive' => empty($this->hasAndBelongsToMany[$assoc]['conditions']) ? -1 : 0,
@@ -1456,7 +1458,7 @@ function __saveMulti($joined, $id, &$db) {
}
if (!empty($newValues)) {
- $fields = implode(',', $fields);
+ $fields = implode(',', $fields);
$db->insertMulti($this->{$join}, $fields, $newValues);
}
}
View
@@ -936,6 +936,9 @@ function combine($data, $path1 = null, $path2 = null, $groupPath = null) {
} else {
$keys = Set::extract($data, $path1);
}
+ if (empty($keys)) {
+ return array();
+ }
if (!empty($path2) && is_array($path2)) {
$format = array_shift($path2);
@@ -967,7 +970,9 @@ function combine($data, $path1 = null, $path2 = null, $groupPath = null) {
return $out;
}
}
-
+ if (empty($vals)) {
+ return array();
+ }
return array_combine($keys, $vals);
}
@@ -3352,6 +3352,7 @@ function testResetAddedAssociation() {
$this->assertTrue(empty($this->Article->hasMany['ArticlesTag']));
}
+
/**
* testResetAssociation method
*
@@ -166,8 +166,8 @@ class DboPostgresTest extends CakeTestCase {
* @access public
*/
var $fixtures = array('core.user', 'core.binary_test', 'core.comment', 'core.article',
- 'core.tag', 'core.articles_tag', 'core.attachment', 'core.person', 'core.post', 'core.author');
-
+ 'core.tag', 'core.articles_tag', 'core.attachment', 'core.person', 'core.post', 'core.author',
+ );
/**
* Actual DB connection used in testing
*
@@ -503,16 +503,23 @@ function testCakeSchema() {
)');
$model =& ClassRegistry::init('datatypes');
$schema = new CakeSchema(array('connection' => 'test_suite'));
- $result = $schema->read(array('connection' => 'test_suite'));
- $schema->tables = $result['tables']['missing'];
+ $result = $schema->read(array(
+ 'connection' => 'test_suite',
+ 'models' => array('Datatype')
+ ));
+ $schema->tables = array('datatypes' => $result['tables']['datatypes']);
$result = $db1->createSchema($schema, 'datatypes');
$this->assertNoPattern('/timestamp DEFAULT/', $result);
$this->assertPattern('/timestamp\s*,/', $result);
$db1->query('DROP TABLE ' . $db1->fullTableName('datatypes'));
+
$db1->query($result);
- $result2 = $schema->read(array('connection' => 'test_suite'));
- $schema->tables = $result2['tables']['missing'];
+ $result2 = $schema->read(array(
+ 'connection' => 'test_suite',
+ 'models' => array('Datatype')
+ ));
+ $schema->tables = array('datatypes' => $result2['tables']['datatypes']);
$result2 = $db1->createSchema($schema, 'datatypes');
$this->assertEqual($result, $result2);
@@ -618,6 +618,7 @@ function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() {
));
$this->assertEqual($result['Monkey'], $expected);
}
+
/**
* test that beforeDelete returning false can abort deletion.
*
@@ -634,6 +635,62 @@ function testBeforeDeleteDeleteAbortion() {
$exists = $Model->findById(1);
$this->assertTrue(is_array($exists));
}
+
+/**
+ * test for a habtm deletion error that occurs in postgres but should not.
+ * And should not occur in any dbo.
+ *
+ * @return void
+ */
+ function testDeleteHabtmPostgresFailure() {
+ $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
+
+ $Article =& ClassRegistry::init('Article');
+ $Article->hasAndBelongsToMany['Tag']['unique'] = true;
+
+ $Tag =& ClassRegistry::init('Tag');
+ $Tag->bindModel(array('hasAndBelongsToMany' => array(
+ 'Article' => array(
+ 'className' => 'Article',
+ 'unique' => true
+ )
+ )), true);
+
+ // Article 1 should have Tag.1 and Tag.2
+ $before = $Article->find("all", array(
+ "conditions" => array("Article.id" => 1),
+ ));
+ $this->assertEqual(count($before[0]['Tag']), 2, 'Tag count for Article.id = 1 is incorrect, should be 2 %s');
+
+ // From now on, Tag #1 is only associated with Post #1
+ $submitted_data = array(
+ "Tag" => array("id" => 1, 'tag' => 'tag1'),
+ "Article" => array(
+ "Article" => array(1)
+ )
+ );
+ $Tag->save($submitted_data);
+
+ // One more submission (The other way around) to make sure the reverse save looks good.
+ $submitted_data = array(
+ "Article" => array("id" => 2, 'title' => 'second article'),
+ "Tag" => array(
+ "Tag" => array(2, 3)
+ )
+ );
+ // ERROR:
+ // Postgresql: DELETE FROM "articles_tags" WHERE tag_id IN ('1', '3')
+ // MySQL: DELETE `ArticlesTag` FROM `articles_tags` AS `ArticlesTag` WHERE `ArticlesTag`.`article_id` = 2 AND `ArticlesTag`.`tag_id` IN (1, 3)
+ $Article->save($submitted_data);
+
+ // Want to make sure Article #1 has Tag #1 and Tag #2 still.
+ $after = $Article->find("all", array(
+ "conditions" => array("Article.id" => 1),
+ ));
+
+ // Removing Article #2 from Tag #1 is all that should have happened.
+ $this->assertEqual(count($before[0]["Tag"]), count($after[0]["Tag"]));
+ }
}
?>
@@ -328,6 +328,7 @@ function testCacheClearOnSave() {
$data = array(
'OverallFavorite' => array(
+ 'id' => 22,
'model_type' => '8-track',
'model_id' => '3',
'priority' => '1'
@@ -391,6 +392,7 @@ function testCounterCacheIncrease() {
$User = new CounterCacheUser();
$Post = new CounterCachePost();
$data = array('Post' => array(
+ 'id' => 22,
'title' => 'New Post',
'user_id' => 66
));
@@ -1611,7 +1613,7 @@ function testHabtmSaveWithConditionsInAssociation() {
'DoomedSomethingElse' => array(
'className' => 'SomethingElse',
'joinTable' => 'join_things',
- 'conditions' => 'JoinThing.doomed = 1',
+ 'conditions' => 'JoinThing.doomed = true',
'unique' => true
),
'NotDoomedSomethingElse' => array(
@@ -1653,6 +1653,9 @@ function testCombine() {
$result = Set::combine($b, 'users.{n}.User.id', 'users.{n}.User.non-existant');
$expected = array(2 => null, 14 => null, 25 => null);
$this->assertIdentical($result, $expected);
+
+ $result = Set::combine($a, 'fail', 'fail');
+ $this->assertEqual($result, array());
}
/**

0 comments on commit 4861da6

Please sign in to comment.