mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Clearing HABTM (unique) when HABTM array is empty, refs #2461
This commit is contained in:
parent
4922729432
commit
2dac6d29c2
3 changed files with 47 additions and 1 deletions
|
@ -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]);
|
||||
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue