Skip to content
Permalink
Browse files

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

…no foreign key. Fixes #69.
  • Loading branch information...
jperras committed Sep 9, 2009
1 parent c08153e commit 5a432c6f7156c55b4994887552a018241ebb6fab
@@ -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
))
));
@@ -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
@@ -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);
}
}
@@ -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';
}
?>
@@ -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)
);
}
?>
@@ -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)
);
}
?>

0 comments on commit 5a432c6

Please sign in to comment.
You can’t perform that action at this time.