Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Applying fix from Ceeram for updateAll joins on model relations with …

…no foreign key. Fixes #69.
  • Loading branch information...
commit 5a432c6f7156c55b4994887552a018241ebb6fab 1 parent c08153e
@jperras jperras authored
View
2  cake/libs/model/datasources/dbo_source.php
@@ -1507,7 +1507,7 @@ function _getJoins($model) {
'alias' => $assoc,
'type' => isset($assocData['type']) ? $assocData['type'] : 'LEFT',
'conditions' => trim($this->conditions(
- $this->getConstraint($assocData['association'], $model, $model->{$assoc}, $assoc, $assocData),
+ $this->__mergeConditions($assocData['conditions'], $this->getConstraint($assocData['association'], $model, $model->{$assoc}, $assoc, $assocData)),
true, false, $model
))
));
View
2  cake/tests/cases/libs/model/model.test.php
@@ -69,7 +69,7 @@ class BaseModelTest extends CakeTestCase {
'core.counter_cache_user_nonstandard_primary_key',
'core.counter_cache_post_nonstandard_primary_key', 'core.uuidportfolio',
'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid', 'core.fruit',
- 'core.fruits_uuid_tag', 'core.uuid_tag'
+ 'core.fruits_uuid_tag', 'core.uuid_tag', 'core.product_update_all', 'core.group_update_all'
);
/**
* start method
View
162 cake/tests/cases/libs/model/model_write.test.php
@@ -25,7 +25,6 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
require_once dirname(__FILE__) . DS . 'model.test.php';
-require_once dirname(__FILE__) . DS . 'model_write.test.php';
/**
* ModelWriteTest
*
@@ -3884,6 +3883,167 @@ function testSaveAllHasManyValidationOnly() {
);
$this->assertEqual($TestModel->Comment->validationErrors, $expected);
}
+/**
+ * TestFindAllWithoutForeignKey
+ *
+ * @link http://code.cakephp.org/tickets/view/69
+ * @access public
+ * @return void
+ */
+ function testFindAllForeignKey() {
+ $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
+ $ProductUpdateAll =& new ProductUpdateAll();
+
+ $conditions = array('Group.name' => 'group one');
+
+ $ProductUpdateAll->bindModel(array(
+ 'belongsTo' => array(
+ 'Group' => array('className' => 'GroupUpdateAll')
+ )
+ ));
+
+ $ProductUpdateAll->belongsTo = array(
+ 'Group' => array('className' => 'GroupUpdateAll', 'foreignKey' => 'group_id')
+ );
+
+ $results = $ProductUpdateAll->find('all', compact('conditions'));
+ $this->assertTrue(!empty($results));
+
+ $ProductUpdateAll->bindModel(array('belongsTo'=>array('Group')));
+ $ProductUpdateAll->belongsTo = array(
+ 'Group' => array(
+ 'className' => 'GroupUpdateAll',
+ 'foreignKey' => false,
+ 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
+ ));
+
+ $resultsFkFalse = $ProductUpdateAll->find('all', compact('conditions'));
+ $this->assertTrue(!empty($resultsFkFalse));
+ $expected = array(
+ '0' => array(
+ 'ProductUpdateAll' => array(
+ 'id' => 1,
+ 'name' => 'product one',
+ 'groupcode' => 120,
+ 'group_id' => 1),
+ 'Group' => array(
+ 'id' => 1,
+ 'name' => 'group one',
+ 'code' => 120)
+ ),
+ '1' => array(
+ 'ProductUpdateAll' => array(
+ 'id' => 2,
+ 'name' => 'product two',
+ 'groupcode' => 120,
+ 'group_id' => 1),
+ 'Group' => array(
+ 'id' => 1,
+ 'name' => 'group one',
+ 'code' => 120)
+ )
+
+ );
+ $this->assertEqual($results, $expected);
+ $this->assertEqual($resultsFkFalse, $expected);
+ }
+/**
+ * testProductUpdateAllWithForeignKey
+ *
+ * @link http://code.cakephp.org/tickets/view/69
+ * @access public
+ * @return void
+ */
+ function testProductUpdateAll() {
+ $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
+ $ProductUpdateAll =& new ProductUpdateAll();
+
+ $conditions = array('Group.name' => 'group one');
+
+ $ProductUpdateAll->bindModel(array('belongsTo' => array(
+ 'Group' => array('className' => 'GroupUpdateAll')))
+ );
+
+ $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
+ $results = $ProductUpdateAll->find('all', array(
+ 'conditions' => array('ProductUpdateAll.name' => 'new product')
+ ));
+ $expected = array(
+ '0' => array(
+ 'ProductUpdateAll' => array(
+ 'id' => 1,
+ 'name' => 'new product',
+ 'groupcode' => 120,
+ 'group_id' => 1),
+ 'Group' => array(
+ 'id' => 1,
+ 'name' => 'group one',
+ 'code' => 120)
+ ),
+ '1' => array(
+ 'ProductUpdateAll' => array(
+ 'id' => 2,
+ 'name' => 'new product',
+ 'groupcode' => 120,
+ 'group_id' => 1),
+ 'Group' => array(
+ 'id' => 1,
+ 'name' => 'group one',
+ 'code' => 120)));
+
+ $this->assertEqual($results, $expected);
+ }
+/**
+ * testProductUpdateAllWithoutForeignKey
+ *
+ * @link http://code.cakephp.org/tickets/view/69
+ * @access public
+ * @return void
+ */
+ function testProductUpdateAllWithoutForeignKey() {
+ $this->loadFixtures('ProductUpdateAll', 'GroupUpdateAll');
+ $ProductUpdateAll =& new ProductUpdateAll();
+
+ $conditions = array('Group.name' => 'group one');
+
+ $ProductUpdateAll->bindModel(array('belongsTo' => array(
+ 'Group' => array('className' => 'GroupUpdateAll')
+ )));
+
+ $ProductUpdateAll->belongsTo = array(
+ 'Group' => array(
+ 'className' => 'GroupUpdateAll',
+ 'foreignKey' => false,
+ 'conditions' => 'ProductUpdateAll.groupcode = Group.code'
+ )
+ );
+
+ $ProductUpdateAll->updateAll(array('name' => "'new product'"), $conditions);
+ $resultsFkFalse = $ProductUpdateAll->find('all', array('conditions' => array('ProductUpdateAll.name'=>'new product')));
+ $expected = array(
+ '0' => array(
+ 'ProductUpdateAll' => array(
+ 'id' => 1,
+ 'name' => 'new product',
+ 'groupcode' => 120,
+ 'group_id' => 1),
+ 'Group' => array(
+ 'id' => 1,
+ 'name' => 'group one',
+ 'code' => 120)
+ ),
+ '1' => array(
+ 'ProductUpdateAll' => array(
+ 'id' => 2,
+ 'name' => 'new product',
+ 'groupcode' => 120,
+ 'group_id' => 1),
+ 'Group' => array(
+ 'id' => 1,
+ 'name' => 'group one',
+ 'code' => 120)));
+ $this->assertEqual($resultsFkFalse, $expected);
+ }
}
View
12 cake/tests/cases/libs/model/models.php
@@ -3014,4 +3014,16 @@ class UuidTagNoWith extends CakeTestModel {
);
}
+class ProductUpdateAll extends CakeTestModel {
+ var $name = 'ProductUpdateAll';
+ var $useTable = 'product_update_all';
+
+}
+
+class GroupUpdateAll extends CakeTestModel {
+ var $name = 'GroupUpdateAll';
+ var $useTable = 'group_update_all';
+
+}
+
?>
View
31 cake/tests/fixtures/group_update_all_fixture.php
@@ -0,0 +1,31 @@
+<?php
+class GroupUpdateAllFixture extends CakeTestFixture {
+ var $name = 'GroupUpdateAll';
+ var $table = 'group_update_all';
+
+ var $fields = array(
+ 'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
+ 'name' => array('type'=>'string', 'null' => false, 'length' => 29),
+ 'code' => array('type'=>'integer', 'null' => false, 'length' => 4),
+ 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
+ );
+ var $records = array(
+ array(
+ 'id' => 1,
+ 'name' => 'group one',
+ 'code' => 120),
+ array(
+ 'id' => 2,
+ 'name' => 'group two',
+ 'code' => 125),
+ array(
+ 'id' => 3,
+ 'name' => 'group three',
+ 'code' => 130),
+ array(
+ 'id' => 4,
+ 'name' => 'group four',
+ 'code' => 135)
+ );
+}
+?>
View
37 cake/tests/fixtures/product_update_all_fixture.php
@@ -0,0 +1,37 @@
+<?php
+class ProductUpdateAllFixture extends CakeTestFixture {
+ var $name = 'ProductUpdateAll';
+ var $table = 'product_update_all';
+
+ var $fields = array(
+ 'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
+ 'name' => array('type'=>'string', 'null' => false, 'length' => 29),
+ 'groupcode' => array('type'=>'integer', 'null' => false, 'length' => 4),
+ 'group_id' => array('type'=>'integer', 'null' => false, 'length' => 8),
+ 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1))
+ );
+ var $records = array(
+ array(
+ 'id' => 1,
+ 'name' => 'product one',
+ 'groupcode' => 120,
+ 'group_id' => 1
+ ),
+ array(
+ 'id' => 2,
+ 'name' => 'product two',
+ 'groupcode' => 120,
+ 'group_id' => 1),
+ array(
+ 'id' => 3,
+ 'name' => 'product three',
+ 'groupcode' => 125,
+ 'group_id' => 2),
+ array(
+ 'id' => 4,
+ 'name' => 'product four',
+ 'groupcode' => 135,
+ 'group_id' => 4)
+ );
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.