Browse Source

ResetBehavior enhancements

euromark 12 years ago
parent
commit
84c14faf6d
2 changed files with 110 additions and 3 deletions
  1. 19 2
      Model/Behavior/ResetBehavior.php
  2. 91 1
      Test/Case/Model/Behavior/ResetBehaviorTest.php

+ 19 - 2
Model/Behavior/ResetBehavior.php

@@ -35,6 +35,7 @@ class ResetBehavior extends ModelBehavior {
 		'limit' => 100, // batch of records per loop
 		'timeout' => null, // in seconds
 		'fields' => array(), // if not displayField
+		'updateFields' => array(), // if saved fields should be different from fields
 		'validate' => true, // trigger beforeValidate callback
 		'updateTimestamp' => false, // update modified/updated timestamp
 		'scope' => array(), // optional conditions
@@ -71,7 +72,7 @@ class ResetBehavior extends ModelBehavior {
 			'page' => 1,
 			'limit' => $limit,
 			'fields' => array(),
-			'order' => $Model->alias . '.' . $Model->displayField . ' ASC',
+			'order' => $Model->alias . '.' . $Model->primaryKey . ' ASC',
 			'conditions' => $scope,
 			'recursive' => $recursive,
 		);
@@ -102,12 +103,28 @@ class ResetBehavior extends ModelBehavior {
 		if ($max) {
 			set_time_limit(max($max, $count / $limit));
 		}
+
 		while ($rows = $Model->find('all', $params)) {
 			foreach ($rows as $row) {
 				$Model->create();
 				$fields = $params['fields'];
+				if (!empty($updateFields)) {
+					$fields = $updateFields;
+				}
+				if ($fields && !in_array($Model->primaryKey, $fields)) {
+					$fields[] = $Model->primaryKey;
+				}
+
 				if ($callback) {
-					$Model->{$callback}($row, $fields);
+					if (is_callable($callback)) {
+						$parameters = array(&$row, &$fields);
+						$row = call_user_func_array($callback, $parameters);
+					} else {
+						$row = $Model->{$callback}($row, $fields);
+					}
+					if (!$row) {
+						continue;
+					}
 				}
 
 				$res = $Model->save($row, $validate, $fields);

+ 91 - 1
Test/Case/Model/Behavior/ResetBehaviorTest.php

@@ -65,6 +65,72 @@ class ResetBehaviorTest extends MyCakeTestCase {
 		$this->assertEquals($expected, $x['MyComment']['comment']);
 	}
 
+	public function testResetWithObjectCallback() {
+		$this->Model->Behaviors->unload('Reset');
+		$this->Model->Behaviors->load('Tools.Reset', array('callback' => array($this->Model, 'customObjectCallback')));
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
+
+		$result = $this->Model->resetRecords();
+		$this->assertTrue($result);
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$expected = 'Second Comment for Second Article xxx';
+		$this->assertEquals($expected, $x['MyComment']['comment']);
+	}
+
+	public function testResetWithStaticCallback() {
+		$this->Model->Behaviors->unload('Reset');
+		$this->Model->Behaviors->load('Tools.Reset', array('callback' => 'MyComment::customStaticCallback'));
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
+
+		$result = $this->Model->resetRecords();
+		$this->assertTrue($result);
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$expected = 'Second Comment for Second Article yyy';
+		$this->assertEquals($expected, $x['MyComment']['comment']);
+	}
+
+	public function testResetWithCallbackAndFields() {
+		$this->Model->Behaviors->unload('Reset');
+		$this->Model->Behaviors->load('Tools.Reset', array(
+			'fields' => array('id'),
+			'updateFields' => array('comment'),
+			'callback' => 'MyComment::fieldsCallback'));
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
+
+		$result = $this->Model->resetRecords();
+		$this->assertTrue($result);
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$expected = 'foo';
+		$this->assertEquals($expected, $x['MyComment']['comment']);
+	}
+
+	public function testResetWithCallbackAndFieldsAutoAdded() {
+		$this->Model->Behaviors->unload('Reset');
+		$this->Model->Behaviors->load('Tools.Reset', array(
+			'fields' => array('id'),
+			'updateFields' => array('id'),
+			'callback' => 'MyComment::fieldsCallbackAuto'));
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$this->assertEquals('Second Comment for Second Article', $x['MyComment']['comment']);
+
+		$result = $this->Model->resetRecords();
+		$this->assertTrue($result);
+
+		$x = $this->Model->find('first', array('conditions' => array('id' => 6)));
+		$expected = 'bar';
+		$this->assertEquals($expected, $x['MyComment']['comment']);
+	}
+
 }
 
 class MyComment extends AppModel {
@@ -75,9 +141,33 @@ class MyComment extends AppModel {
 
 	public $displayField = 'comment';
 
-	public function customCallback(&$data, &$fields) {
+	public function customCallback($data, &$updateFields) {
 		$data[$this->alias][$this->displayField] .= ' xyz';
 		$fields[] = 'some_other_field';
+		return $data;
+	}
+
+	public function customObjectCallback($data, &$updateFields) {
+		$data[$this->alias][$this->displayField] .= ' xxx';
+		$updateFields[] = 'some_other_field';
+		return $data;
+	}
+
+	public function customStaticCallback($data, &$updateFields) {
+		$data['MyComment']['comment'] .= ' yyy';
+		$updateFields[] = 'some_other_field';
+		return $data;
+	}
+
+	public function fieldsCallback($data, &$updateFields) {
+		$data['MyComment']['comment'] = 'foo';
+		return $data;
+	}
+
+	public function fieldsCallbackAuto($data, &$updateFields) {
+		$data['MyComment']['comment'] = 'bar';
+		$updateFields[] = 'comment';
+		return $data;
 	}
 
 }