Clearing HABTM (unique) when HABTM array is empty, refs #2461

This commit is contained in:
Jeremy Harris 2014-08-28 17:49:30 -05:00
parent 4922729432
commit 2dac6d29c2
3 changed files with 47 additions and 1 deletions

View file

@ -1201,6 +1201,10 @@ class Model extends Object implements CakeEventListener {
continue;
}
if (!isset($this->data[$modelName])) {
$this->data[$modelName] = array();
}
foreach ($fieldSet as $fieldName => $fieldValue) {
unset($this->validationErrors[$fieldName]);

View file

@ -1724,6 +1724,47 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
}
/**
* test that saving HABTM with an empty array will clear existing HABTM if
* unique is true
*
* @return void
*/
public function testSaveHabtmEmptyData() {
$this->loadFixtures('Node', 'Dependency');
$Node = new Node();
$data = array(
'Node' => array('name' => 'New First')
);
$Node->id = 1;
$Node->save($data);
$node = $Node->find('first', array(
'conditions' => array('Node.id' => 1),
'contain' => array('ParentNode')
));
$result = Hash::extract($node, 'ParentNode.{n}.id');
$expected = array(2);
$this->assertEquals($expected, $result);
$data = array(
'ParentNode' => array()
);
$Node->id = 1;
$Node->save($data);
$node = $Node->find('first', array(
'conditions' => array('Node.id' => 1),
'contain' => array('ParentNode')
));
$result = Hash::extract($node, 'ParentNode.{n}.id');
$expected = array();
$this->assertEquals($expected, $result);
}
/**
* testSaveHabtmNoPrimaryData method
*

View file

@ -30,6 +30,7 @@ class DependencyFixture extends CakeTestFixture {
* @var array
*/
public $fields = array(
'id' => 'integer',
'child_id' => 'integer',
'parent_id' => 'integer'
);
@ -40,6 +41,6 @@ class DependencyFixture extends CakeTestFixture {
* @var array
*/
public $records = array(
array('child_id' => 1, 'parent_id' => 2),
array('id' => 1, 'child_id' => 1, 'parent_id' => 2),
);
}