浏览代码

ResetBehavior

euromark 13 年之前
父节点
当前提交
485a357baa
共有 2 个文件被更改,包括 136 次插入0 次删除
  1. 88 0
      Model/Behavior/ResetBehavior.php
  2. 48 0
      Test/Case/Model/Behavior/ResetBehaviorTest.php

+ 88 - 0
Model/Behavior/ResetBehavior.php

@@ -0,0 +1,88 @@
+<?php
+App::uses('ModelBehavior', 'Model');
+
+/**
+ *
+ * @author Mark Scherer
+ * @license MIT
+ * @cakephp 2.0
+ * @version 1
+ * 2011-12-06 ms
+ */
+class ResetBehavior extends ModelBehavior {
+
+	protected $_defaults = array(
+		'limit' => 100,
+		'auto' => false,
+		'fields' => array(),
+		'model' => null,
+		'notices' => true,
+		'validate' => true,
+	);
+
+	/**
+	 * Configure the behavior through the Model::actsAs property
+	 *
+	 * @param object $Model
+	 * @param array $config
+	 */
+	public function setup(Model $Model, $config = null) {
+		if (is_array($config)) {
+			$this->settings[$Model->alias] = array_merge($this->_defaults, $config);
+		} else {
+			$this->settings[$Model->alias] = $this->_defaults;
+		}
+	}
+
+	/**
+	 * resetRecords method
+	 *
+	 * Regenerate all records (run beforeValidate/beforeSave callbacks).
+	 *
+	 * @param Model $Model
+	 * @param array $conditions
+	 * @param int $recursive
+	 * @return bool true on success false otherwise
+	 */
+	public function resetRecords(Model $Model, $params = array()) {
+		$recursive = -1;
+		extract($this->settings[$Model->alias]);
+		/*
+		if ($notices && !$Model->hasField($fields)) {
+			return false;
+		}
+		*/
+		$defaults = array(
+			'page' => 1,
+			'limit' => $limit,
+			'fields' => array(),
+			'order' => $Model->alias.'.'.$Model->displayField . ' ASC',
+			'conditions' => array(),
+			'recursive' => $recursive,
+		);
+		if (!empty($fields)) {
+			$defaults['fields'] = array_merge(array($Model->primaryKey), $fields);
+		} else {
+			$defaults['fields'] = array($Model->primaryKey, $Model->displayField);
+		}
+
+		$params = array_merge($defaults, $params);
+		$count = $Model->find('count', compact('conditions'));
+		$max = ini_get('max_execution_time');
+		if ($max) {
+			set_time_limit (max($max, $count / $limit));
+		}
+		while ($rows = $Model->find('all', $params)) {
+			foreach ($rows as $row) {
+				$Model->create();
+				$res = $Model->save($row, $validate, $params['fields']);
+				if (!$res) {
+					throw new CakeException(print_r($Model->validationErrors, true));
+				}
+			}
+			$params['page']++;
+		}
+		return true;
+	}
+
+}

+ 48 - 0
Test/Case/Model/Behavior/ResetBehaviorTest.php

@@ -0,0 +1,48 @@
+<?php
+
+App::uses('ResetBehavior', 'Tools.Model/Behavior');
+App::uses('MyCakeTestCase', 'Tools.TestSuite');
+App::uses('AppModel', 'Model');
+
+class ResetBehaviorTest extends MyCakeTestCase {
+
+	public $ResetBehavior;
+
+	public $Model;
+
+	public $fixtures = array('core.comment');
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->ResetBehavior = new ResetBehavior();
+
+		$this->Model = ClassRegistry::init('MyComment');
+		$this->Model->Behaviors->attach('Tools.Reset');
+	}
+
+	public function testObject() {
+		$this->assertTrue(is_object($this->ResetBehavior));
+		$this->assertInstanceOf('ResetBehavior', $this->ResetBehavior);
+	}
+
+	public function testResetRecords() {
+		$x = $this->Model->find('first', array('order' => array('updated'=>'DESC')));
+		$this->assertTrue($x['MyComment']['updated'] < '2007-12-31');
+
+		$result = $this->Model->resetRecords();
+		$this->assertTrue($result);
+
+		$x = $this->Model->find('first', array('order' => array('updated'=>'ASC')));
+		$this->assertTrue($x['MyComment']['updated'] > (date('Y')-1) . '-12-31');
+	}
+
+}
+
+class MyComment extends AppModel {
+
+	public $fixture = 'core.comment';
+
+	public $useTable = 'comments';
+
+}