mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Allow afterSave() callbacks to modify value of a data field, as per ticket #2465\nNecessary tests included
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6889 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
ab0a607189
commit
52037d15e4
2 changed files with 46 additions and 17 deletions
|
@ -1166,7 +1166,7 @@ class Model extends Overloadable {
|
||||||
$this->afterSave($created);
|
$this->afterSave($created);
|
||||||
}
|
}
|
||||||
if (!empty($this->data)) {
|
if (!empty($this->data)) {
|
||||||
$success = Set::pushDiff($success, $this->data);
|
$success = Set::merge($success, $this->data);
|
||||||
}
|
}
|
||||||
$this->data = false;
|
$this->data = false;
|
||||||
$this->__exists = null;
|
$this->__exists = null;
|
||||||
|
@ -2607,4 +2607,4 @@ class Model extends Overloadable {
|
||||||
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
|
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
|
||||||
Overloadable::overload('Model');
|
Overloadable::overload('Model');
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -77,13 +77,27 @@ class TestBehavior extends ModelBehavior {
|
||||||
}
|
}
|
||||||
|
|
||||||
function afterSave(&$model, $created) {
|
function afterSave(&$model, $created) {
|
||||||
parent::afterSave($model, $created);
|
|
||||||
$settings = $this->settings[$model->alias];
|
$settings = $this->settings[$model->alias];
|
||||||
if (isset($settings['afterSave']) && $settings['afterSave'] == 'on') {
|
if (!isset($settings['afterSave']) || $settings['afterSave'] == 'off') {
|
||||||
$model->data[$model->alias]['aftersave'] = 'modified';
|
return parent::afterSave($model, $created);
|
||||||
if ($created) {
|
}
|
||||||
$model->data[$model->alias]['aftersave'] .= ' on create';
|
$string = 'modified after';
|
||||||
}
|
if ($created) {
|
||||||
|
$string .= ' on create';
|
||||||
|
}
|
||||||
|
switch ($settings['afterSave']) {
|
||||||
|
case 'on':
|
||||||
|
$model->data[$model->alias]['aftersave'] = $string;
|
||||||
|
break;
|
||||||
|
case 'test':
|
||||||
|
unset($model->data[$model->alias]['name']);
|
||||||
|
break;
|
||||||
|
case 'test2':
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case 'modify':
|
||||||
|
$model->data[$model->alias]['name'] .= ' ' . $string;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,22 +317,37 @@ class BehaviorTest extends CakeTestCase {
|
||||||
|
|
||||||
$Sample->Behaviors->disable('Test');
|
$Sample->Behaviors->disable('Test');
|
||||||
$this->assertIdentical($Sample->save($record), $record);
|
$this->assertIdentical($Sample->save($record), $record);
|
||||||
|
|
||||||
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'on'));
|
|
||||||
$expected = Set::merge($record, array('Sample' => array('name' => 'sample99 modified before', 'aftersave' => 'modified on create')));
|
|
||||||
$Sample->create();
|
|
||||||
$this->assertIdentical($Sample->save($record), $expected);
|
|
||||||
|
|
||||||
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
|
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
|
||||||
$expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified on create')));
|
$expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
|
||||||
$Sample->create();
|
$Sample->create();
|
||||||
$this->assertIdentical($Sample->save($record), $expected);
|
$this->assertIdentical($Sample->save($record), $expected);
|
||||||
|
|
||||||
$Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
|
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
|
||||||
$record = $Sample->read(null, 1);
|
$expected = Set::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
|
||||||
$expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified')));
|
|
||||||
$Sample->create();
|
$Sample->create();
|
||||||
$this->assertIdentical($Sample->save($record), $expected);
|
$this->assertIdentical($Sample->save($record), $expected);
|
||||||
|
|
||||||
|
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
|
||||||
|
$Sample->create();
|
||||||
|
$this->assertIdentical($Sample->save($record), $record);
|
||||||
|
|
||||||
|
$Sample->Behaviors->attach('Test', array('afterSave' => 'test2'));
|
||||||
|
$Sample->create();
|
||||||
|
$this->assertIdentical($Sample->save($record), $record);
|
||||||
|
|
||||||
|
$Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
|
||||||
|
$record2 = $Sample->read(null, 1);
|
||||||
|
|
||||||
|
$Sample->Behaviors->attach('Test', array('afterSave' => 'on'));
|
||||||
|
$expected = Set::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
|
||||||
|
$Sample->create();
|
||||||
|
$this->assertIdentical($Sample->save($record2), $expected);
|
||||||
|
|
||||||
|
$Sample->Behaviors->attach('Test', array('afterSave' => 'modify'));
|
||||||
|
$expected = Set::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
|
||||||
|
$Sample->create();
|
||||||
|
$this->assertIdentical($Sample->save($record2), $expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testBehaviorValidateCallback() {
|
function testBehaviorValidateCallback() {
|
||||||
|
|
Loading…
Reference in a new issue