euromark 12 年之前
父节点
当前提交
2c1ddfd108
共有 1 个文件被更改,包括 7 次插入6 次删除
  1. 7 6
      Model/Behavior/NamedScopeBehavior.php

+ 7 - 6
Model/Behavior/NamedScopeBehavior.php

@@ -15,7 +15,7 @@ App::uses('ModelBehavior', 'Model');
  * - reads the model's 'scopes' attribute if applicable
  * - reads the model's 'scopes' attribute if applicable
  * - allows 'scopes' in scopedFind()
  * - allows 'scopes' in scopedFind()
  *
  *
- * If used across models, it is adviced to load this globally via $actAs in the AppModel
+ * If used across models, it is advisable to load this globally via $actAs in the AppModel
  * (just as with Containable).
  * (just as with Containable).
  *
  *
  * In case you need to dynamically set the Model->scopes attribute, use the constructor:
  * In case you need to dynamically set the Model->scopes attribute, use the constructor:
@@ -34,6 +34,7 @@ App::uses('ModelBehavior', 'Model');
  *
  *
  * @license MIT
  * @license MIT
  * @author Mark Scherer
  * @author Mark Scherer
+ * @link https://github.com/dereuromark/tools/wiki/Model-Behavior-NamedScope
  */
  */
 class NamedScopeBehavior extends ModelBehavior {
 class NamedScopeBehavior extends ModelBehavior {
 
 
@@ -67,21 +68,21 @@ class NamedScopeBehavior extends ModelBehavior {
 	 */
 	 */
 	public function beforeFind(Model $Model, $queryData) {
 	public function beforeFind(Model $Model, $queryData) {
 		$scopes = array();
 		$scopes = array();
-		// passed as scopes
+		// Passed as scopes (preferred)
 		if (!empty($queryData['scope'])) {
 		if (!empty($queryData['scope'])) {
 			$scope = !is_array($queryData['scope']) ? array($queryData['scope']) : $queryData['scope'];
 			$scope = !is_array($queryData['scope']) ? array($queryData['scope']) : $queryData['scope'];
 			$scopes = array_merge($scopes, $scope);
 			$scopes = array_merge($scopes, $scope);
 		}
 		}
-		// passed as conditions['scope']
+		// Passed as conditions['scope']
 		if (is_array($queryData['conditions']) && !empty($queryData['conditions']['scope'])) {
 		if (is_array($queryData['conditions']) && !empty($queryData['conditions']['scope'])) {
 			$scope = !is_array($queryData['conditions']['scope']) ? array($queryData['conditions']['scope']) : $queryData['conditions']['scope'];
 			$scope = !is_array($queryData['conditions']['scope']) ? array($queryData['conditions']['scope']) : $queryData['conditions']['scope'];
 			unset($queryData['conditions']['scope']);
 			unset($queryData['conditions']['scope']);
 			$scopes = array_merge($scopes, $scope);
 			$scopes = array_merge($scopes, $scope);
 		}
 		}
 
 
-		// if there are scopes defined, we need to get rid of possible condition set earlier by find() method if model->id was set
-		if (!empty($scopes) && !empty($Model->id) && !empty($queryData['conditions']["`{$Model->alias}`.`{$Model->primaryKey}`"]) && $queryData['conditions']["`{$Model->alias}`.`{$Model->primaryKey}`"] ==
-			$Model->id) {
+		// If there are scopes defined, we need to get rid of possible condition set earlier by find() method if model->id was set
+		if (!empty($scopes) && !empty($Model->id) && !empty($queryData['conditions']["`{$Model->alias}`.`{$Model->primaryKey}`"]) &&
+			$queryData['conditions']["`{$Model->alias}`.`{$Model->primaryKey}`"] == $Model->id) {
 			unset($queryData['conditions']["`{$Model->alias}`.`{$Model->primaryKey}`"]);
 			unset($queryData['conditions']["`{$Model->alias}`.`{$Model->primaryKey}`"]);
 		}
 		}