浏览代码

Use shims

Mark Scherer 11 年之前
父节点
当前提交
d8a4614b13
共有 4 个文件被更改,包括 16 次插入83 次删除
  1. 9 60
      Model/MyModel.php
  2. 2 2
      View/Helper/FormExtHelper.php
  3. 2 2
      View/Helper/HtmlExtHelper.php
  4. 3 19
      docs/Shims.md

+ 9 - 60
Model/MyModel.php

@@ -1226,71 +1226,20 @@ class MyModel extends ShimModel {
 
 	/**
 	 * Shortcut method to find a specific entry via primary key.
-	 *
-	 * Either provide the id directly:
+	 * Wraps ShimModel::get() for an exception free response.
 	 *
 	 *   $record = $this->Model->get($id);
 	 *
-	 * Or use
-	 *
-	 *   $this->Model->id = $id;
-	 *   $record = $this->Model->get();
-	 *
 	 * @param mixed $id
-	 * @param array $options Options for find(). Used to be fields array/string.
-	 * @param array $contain Deprecated - use
-	 * @return mixed
+	 * @param array $options Options for find().
+	 * @return array
 	 */
-	public function get($id = null, $options = [], $contain = []) {
-		if (is_array($id)) {
-			$column = $id[0];
-			$value = $id[1];
-		} else {
-			$column = $this->primaryKey;
-			$value = $id;
-			if ($value === null) {
-				$value = $this->id;
-			}
-		}
-		if (!$value) {
-			return [];
-		}
-
-		// BC
-		$fields = null;
-		if (is_string($options)) {
-			$fields = $options;
-			$options = [];
+	public function record($id, array $options = []) {
+		try {
+			return $this->get($id, $options);
+		} catch (RecordNotFoundException $e) {
 		}
-		if (!empty($options) && !array_key_exists('fields', $options) && !array_key_exists('contain', $options)) {
-			$fields = $options;
-			$options = [];
-		}
-
-		if ($fields === '*') {
-			$fields = $this->alias . '.*';
-		} elseif (!empty($fields)) {
-			foreach ((array)$fields as $row => $field) {
-				if (strpos($field, '.') !== false) {
-					continue;
-				}
-				$fields[$row] = $this->alias . '.' . $field;
-			}
-		}
-
-		$options = [
-			'conditions' => [$this->alias . '.' . $column => $value],
-		] + $options;
-
-		// BC
-		if (!empty($fields)) {
-			$options['fields'] = $fields;
-		}
-		if (!empty($contain)) {
-			$options['contain'] = $contain;
-		}
-
-		return $this->find('first', $options);
+		return array();
 	}
 
 	/**
@@ -1362,7 +1311,7 @@ class MyModel extends ShimModel {
 	 * @return ARRAY record: [Model][values],...
 	 */
 	public function toggleField($fieldName, $id) {
-		$record = $this->get($id, [$this->primaryKey, $fieldName]);
+		$record = $this->get($id, ['conditions' => [$this->primaryKey, $fieldName]]);
 
 		if (!empty($record) && !empty($fieldName) && $this->hasField($fieldName)) {
 			$record[$this->alias][$fieldName] = ($record[$this->alias][$fieldName] == 1 ? 0 : 1);

+ 2 - 2
View/Helper/FormExtHelper.php

@@ -1,5 +1,5 @@
 <?php
-App::uses('FormHelper', 'View/Helper');
+App::uses('FormShimHelper', 'Shim.View/Helper');
 
 /**
  * Enhance Forms with JS widget stuff
@@ -20,7 +20,7 @@ App::uses('FormHelper', 'View/Helper');
  *   $this->Js->writeBuffer() with onDomReady=>false then, though.
  *
  */
-class FormExtHelper extends FormHelper {
+class FormExtHelper extends FormShimHelper {
 
 	public $helpers = ['Html', 'Js', 'Tools.Common'];
 

+ 2 - 2
View/Helper/HtmlExtHelper.php

@@ -1,5 +1,5 @@
 <?php
-App::uses('HtmlHelper', 'View/Helper');
+App::uses('HtmlShimHelper', 'Shim.View/Helper');
 
 /**
  * HtmlExt Helper
@@ -10,7 +10,7 @@ App::uses('HtmlHelper', 'View/Helper');
  * @author Mark Scherer
  * @license http://opensource.org/licenses/mit-license.php MIT
  */
-class HtmlExtHelper extends HtmlHelper {
+class HtmlExtHelper extends HtmlShimHelper {
 
 	/**
 	 * For convenience functions Html::defaultLink() and defaultUrl().

+ 3 - 19
docs/Shims.md

@@ -22,7 +22,9 @@ $config['Paginator'] = array(
 ```
 Just make sure, that your AppController extends the `Tools.MyController` class.
 
-You can also set `Configure::write('App.warnAboutNamedParams', true)` to get a warning if
+Tip: Using the Shim plugin this is already done for you out of the box.
+
+You can also set `Configure::write('Shim.warnAboutNamedParams', true)` to get a warning if
 any of your code still uses the deprecated named params.
 
 
@@ -30,12 +32,6 @@ any of your code still uses the deprecated named params.
 In 3.x there will be a FlashComponent instead. Mine also provides stackable (multi) messages.
 See https://github.com/dereuromark/cakephp-tools/wiki/Flash-messages for details.
 
-### IntegrationTestCase
-The ControllerTestCase will be gone in 3.x. Instead there will be a IntegrationTestCase that involves
-less, but smarter, mocking.
-I backported a basic version of it in 2.x already.
-When upgrading you almost have nothing to change in those test case files.
-
 ### Login/Passwords
 Already use the PHP5.5+ password functionality with the ModernPasswordHasher class and the Passwordable behavior.
 Easily upgradable to 3.x in minutes.
@@ -43,18 +39,6 @@ Easily upgradable to 3.x in minutes.
 ### RSS
 Use RssView instead of the akward and limited helper approach.
 
-### MyCakeTestCase
-- testAction() defaults to GET
-- TestConsoleOutput() for stdout and stderr instead of mocks
-
-### Model shims
-In 3.x the updateAll() and deleteAll() won't autojoin anymore. In order to already write future proof versions of that you can use
-- updateAllJoinless()
-- deleteAllJoinless()
-In 3.x all you need to do is rename them back again instead of tryig to fix all broken code.
-
-For findById() and exists() there will be get(). You can directly use it via MyModel::get().
-
 
 ### More