Merge pull request #7086 from cakephp/issue-7069

Fix issues saveMany & saveAssociated with boolean values.
This commit is contained in:
Mark Story 2015-07-22 22:01:39 -04:00
commit e19f621314
2 changed files with 94 additions and 4 deletions

View file

@ -1717,7 +1717,7 @@ class Model extends Object implements CakeEventListener {
* - validate: Set to true/false to enable or disable validation. * - validate: Set to true/false to enable or disable validation.
* - fieldList: An array of fields you want to allow for saving. * - fieldList: An array of fields you want to allow for saving.
* - callbacks: Set to false to disable callbacks. Using 'before' or 'after' * - callbacks: Set to false to disable callbacks. Using 'before' or 'after'
* will enable only those callbacks. * will enable only those callbacks.
* - `counterCache`: Boolean to control updating of counter caches (if any) * - `counterCache`: Boolean to control updating of counter caches (if any)
* *
* @param array $fieldList List of fields to allow to be saved * @param array $fieldList List of fields to allow to be saved
@ -2335,7 +2335,7 @@ class Model extends Object implements CakeEventListener {
if ($options['deep']) { if ($options['deep']) {
$saved = $this->saveAssociated($record, array('atomic' => false) + $options); $saved = $this->saveAssociated($record, array('atomic' => false) + $options);
} else { } else {
$saved = $this->save($record, array('atomic' => false) + $options); $saved = (bool)$this->save($record, array('atomic' => false) + $options);
} }
} }
@ -2478,7 +2478,7 @@ class Model extends Object implements CakeEventListener {
if ($options['deep']) { if ($options['deep']) {
$saved = $Model->saveAssociated($values, array('atomic' => false) + $options); $saved = $Model->saveAssociated($values, array('atomic' => false) + $options);
} else { } else {
$saved = $Model->save($values, array('atomic' => false) + $options); $saved = (bool)$Model->save($values, array('atomic' => false) + $options);
} }
$validates = ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true))); $validates = ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true)));
} }
@ -2534,7 +2534,7 @@ class Model extends Object implements CakeEventListener {
if ($options['deep']) { if ($options['deep']) {
$saved = $Model->saveAssociated($values, array('atomic' => false) + $options); $saved = $Model->saveAssociated($values, array('atomic' => false) + $options);
} else { } else {
$saved = $Model->save($values, $options); $saved = (bool)$Model->save($values, $options);
} }
} }

View file

@ -7684,6 +7684,37 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals(2, count($result['Attachment'])); $this->assertEquals(2, count($result['Attachment']));
} }
/**
* Test that boolean fields don't cause saveMany to fail
*
* @return void
*/
public function testSaveManyBooleanFields() {
$this->loadFixtures('Item', 'Syfile', 'Image');
$data = array(
array(
'Item' => array(
'name' => 'testing',
'syfile_id' => 1,
'published' => false
)
),
array(
'Item' => array(
'name' => 'testing 2',
'syfile_id' => 1,
'published' => true
)
),
);
$item = ClassRegistry::init('Item');
$result = $item->saveMany($data, array('atomic' => false));
$this->assertCount(2, $result, '2 records should have been saved.');
$this->assertTrue($result[0], 'Both should have succeded');
$this->assertTrue($result[1], 'Both should have succeded');
}
/** /**
* testSaveManyDeepHasManyValidationFailure method * testSaveManyDeepHasManyValidationFailure method
* *
@ -7806,6 +7837,65 @@ class ModelWriteTest extends BaseModelTest {
), $TestModel->validationErrors); ), $TestModel->validationErrors);
} }
/**
* Test that boolean fields don't cause saveAssociated to fail
*
* @return void
*/
public function testSaveAssociatedHasOneBooleanFields() {
$this->loadFixtures('Item', 'Syfile', 'Image');
$data = array(
'Syfile' => array(
'image_id' => 1,
'name' => 'Some file',
),
'Item' => array(
'name' => 'testing',
'published' => false
),
);
$syfile = ClassRegistry::init('Syfile');
$syfile->bindModel(array('hasOne' => array('Item')), false);
$result = $syfile->saveAssociated($data, array('atomic' => false));
$this->assertCount(2, $result, '2 records should have been saved.');
$this->assertTrue($result['Syfile'], 'Both should have succeded');
$this->assertTrue($result['Item'], 'Both should have succeded');
}
/**
* Test that boolean fields don't cause saveAssociated to fail
*
* @return void
*/
public function testSaveAssociatedBelongsToBooleanFields() {
$this->loadFixtures('Item', 'Syfile', 'Image');
$data = array(
'Syfile' => array(
'image_id' => 1,
'name' => 'Some file',
),
'Item' => array(
'name' => 'testing',
'syfile_id' => 2,
'published' => false
),
);
$item = ClassRegistry::init('Item');
$item->bindModel(array(
'belongsTo' => array(
'Item' => array(
'foreignKey' => 'image_id'
)
)
), false);
$result = $item->saveAssociated($data, array('atomic' => false));
$this->assertCount(2, $result, '2 records should have been saved.');
$this->assertTrue($result['Syfile'], 'Both should have succeded');
$this->assertTrue($result['Item'], 'Both should have succeded');
}
/** /**
* testUpdateAllBoolean * testUpdateAllBoolean
* *