From 2d1180f35caeaab90353b445e5720f7df61ff286 Mon Sep 17 00:00:00 2001 From: mdeweerd Date: Mon, 6 May 2013 09:56:15 +0300 Subject: [PATCH 1/3] Add ensureBehaviors() to detachBehavior*() Need to add ensureBehaviors() to detachBehavior*(). The intent may be to detach behaviors defined in behaviors(). In the original implementation, if these behaviors() are not yet attached, they can not selectively be detached. --- framework/base/Component.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/framework/base/Component.php b/framework/base/Component.php index 80259e7..582cf03 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -496,6 +496,7 @@ class Component extends Object */ public function detachBehavior($name) { + $this->ensureBehaviors(); if (isset($this->_behaviors[$name])) { $behavior = $this->_behaviors[$name]; unset($this->_behaviors[$name]); @@ -511,6 +512,7 @@ class Component extends Object */ public function detachBehaviors() { + $this->ensureBehaviors(); if ($this->_behaviors !== null) { foreach ($this->_behaviors as $name => $behavior) { $this->detachBehavior($name); From e9ad7d6e367b0a6c1816c3a5f5b6a863ffa37ba2 Mon Sep 17 00:00:00 2001 From: resurtm Date: Mon, 6 May 2013 17:46:34 +0600 Subject: [PATCH 2/3] Rollback word consistencty over entire codebase (ref. #139). --- framework/db/Connection.php | 2 +- framework/db/Migration.php | 8 ++++---- framework/db/Transaction.php | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/framework/db/Connection.php b/framework/db/Connection.php index f71b940..03b10a8 100644 --- a/framework/db/Connection.php +++ b/framework/db/Connection.php @@ -66,7 +66,7 @@ use yii\caching\Cache; * // ... executing other SQL statements ... * $transaction->commit(); * } catch(Exception $e) { - * $transaction->rollBack(); + * $transaction->rollback(); * } * ~~~ * diff --git a/framework/db/Migration.php b/framework/db/Migration.php index 88abab5..f51e597 100644 --- a/framework/db/Migration.php +++ b/framework/db/Migration.php @@ -64,14 +64,14 @@ class Migration extends \yii\base\Component $transaction = $this->db->beginTransaction(); try { if ($this->safeUp() === false) { - $transaction->rollBack(); + $transaction->rollback(); return false; } $transaction->commit(); } catch (\Exception $e) { echo "Exception: " . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n"; echo $e->getTraceAsString() . "\n"; - $transaction->rollBack(); + $transaction->rollback(); return false; } return null; @@ -89,14 +89,14 @@ class Migration extends \yii\base\Component $transaction = $this->db->beginTransaction(); try { if ($this->safeDown() === false) { - $transaction->rollBack(); + $transaction->rollback(); return false; } $transaction->commit(); } catch (\Exception $e) { echo "Exception: " . $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ")\n"; echo $e->getTraceAsString() . "\n"; - $transaction->rollBack(); + $transaction->rollback(); return false; } return null; diff --git a/framework/db/Transaction.php b/framework/db/Transaction.php index d66c38e..195a8c8 100644 --- a/framework/db/Transaction.php +++ b/framework/db/Transaction.php @@ -25,7 +25,7 @@ use yii\base\InvalidConfigException; * //.... other SQL executions * $transaction->commit(); * } catch(Exception $e) { - * $transaction->rollBack(); + * $transaction->rollback(); * } * ~~~ * @@ -42,14 +42,14 @@ class Transaction extends \yii\base\Object public $db; /** * @var boolean whether this transaction is active. Only an active transaction - * can [[commit()]] or [[rollBack()]]. This property is set true when the transaction is started. + * can [[commit()]] or [[rollback()]]. This property is set true when the transaction is started. */ private $_active = false; /** * Returns a value indicating whether this transaction is active. * @return boolean whether this transaction is active. Only an active transaction - * can [[commit()]] or [[rollBack()]]. + * can [[commit()]] or [[rollback()]]. */ public function getIsActive() { From 034d88e5e480d2a681f336acc3b0bdd7c758cc42 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 6 May 2013 08:04:35 -0400 Subject: [PATCH 3/3] Fixes issue #134 --- docs/api/db/ActiveRecord.md | 4 ++-- framework/db/ActiveQuery.php | 3 ++- tests/unit/data/ar/Customer.php | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/api/db/ActiveRecord.md b/docs/api/db/ActiveRecord.md index 822c548..4e82793 100644 --- a/docs/api/db/ActiveRecord.md +++ b/docs/api/db/ActiveRecord.md @@ -412,7 +412,7 @@ class Customer extends \yii\db\ActiveRecord /** * @param ActiveQuery $query */ - public function active($query) + public static function active($query) { $query->andWhere('status = 1'); } @@ -435,7 +435,7 @@ class Customer extends \yii\db\ActiveRecord * @param ActiveQuery $query * @param integer $age */ - public function olderThan($query, $age = 30) + public static function olderThan($query, $age = 30) { $query->andWhere('age > :age', array(':age' => $age)); } diff --git a/framework/db/ActiveQuery.php b/framework/db/ActiveQuery.php index 43c3059..3999600 100644 --- a/framework/db/ActiveQuery.php +++ b/framework/db/ActiveQuery.php @@ -88,7 +88,8 @@ class ActiveQuery extends Query { if (method_exists($this->modelClass, $name)) { array_unshift($params, $this); - return call_user_func_array(array($this->modelClass, $name), $params); + call_user_func_array(array($this->modelClass, $name), $params); + return $this; } else { return parent::__call($name, $params); } diff --git a/tests/unit/data/ar/Customer.php b/tests/unit/data/ar/Customer.php index 9b3eab6..b26b51b 100644 --- a/tests/unit/data/ar/Customer.php +++ b/tests/unit/data/ar/Customer.php @@ -22,6 +22,6 @@ class Customer extends ActiveRecord public static function active($query) { - return $query->andWhere('status=1'); + $query->andWhere('status=1'); } }