Applying fix from Ceeram for updateAll joins on model relations with no foreign key. Fixes #69.

This commit is contained in:
jperras 2009-09-08 23:48:32 -04:00
parent c08153e1bd
commit 5a432c6f71
6 changed files with 243 additions and 3 deletions

View file

@ -1507,7 +1507,7 @@ class DboSource extends DataSource {
'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 file

@ -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 file

@ -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 @@ class ModelWriteTest extends BaseModelTest {
);
$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 file

@ -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 file

@ -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 file

@ -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)
);
}
?>