From 83527e85ca5dea8a66db4f8f256abc38bf150229 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 28 Nov 2013 01:00:05 +0100 Subject: [PATCH] made Model::attributes() non static again - allows to have dynamic definition of attributes depended on the instance - there was no real need for it to be static. Places that used it static have been refactored. fixes #1318 --- extensions/elasticsearch/ActiveRecord.php | 2 +- extensions/redis/ActiveRecord.php | 2 +- extensions/sphinx/ActiveRecord.php | 2 +- framework/yii/base/Model.php | 4 ++-- framework/yii/db/ActiveRecord.php | 4 ++-- framework/yii/validators/UniqueValidator.php | 5 ----- tests/unit/data/ar/elasticsearch/Customer.php | 2 +- tests/unit/data/ar/elasticsearch/Item.php | 2 +- tests/unit/data/ar/elasticsearch/Order.php | 2 +- tests/unit/data/ar/elasticsearch/OrderItem.php | 2 +- tests/unit/data/ar/redis/Customer.php | 2 +- tests/unit/data/ar/redis/Item.php | 2 +- tests/unit/data/ar/redis/Order.php | 2 +- tests/unit/data/ar/redis/OrderItem.php | 4 +--- 14 files changed, 15 insertions(+), 22 deletions(-) diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index e5f991f..efdb5fe 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -216,7 +216,7 @@ class ActiveRecord extends \yii\db\ActiveRecord * This method must be overridden by child classes to define available attributes. * @return array list of attribute names. */ - public static function attributes() + public function attributes() { throw new InvalidConfigException('The attributes() method of elasticsearch ActiveRecord has to be implemented by child classes.'); } diff --git a/extensions/redis/ActiveRecord.php b/extensions/redis/ActiveRecord.php index 1137629..8c2933c 100644 --- a/extensions/redis/ActiveRecord.php +++ b/extensions/redis/ActiveRecord.php @@ -81,7 +81,7 @@ class ActiveRecord extends \yii\db\ActiveRecord * This method must be overridden by child classes to define available attributes. * @return array list of attribute names. */ - public static function attributes() + public function attributes() { throw new InvalidConfigException('The attributes() method of redis ActiveRecord has to be implemented by child classes.'); } diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index d7f44ef..9cf03ff 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -623,7 +623,7 @@ abstract class ActiveRecord extends Model * The default implementation will return all column names of the table associated with this AR class. * @return array list of attribute names. */ - public static function attributes() + public function attributes() { return array_keys(static::getIndexSchema()->columns); } diff --git a/framework/yii/base/Model.php b/framework/yii/base/Model.php index 06f0d1f..3668826 100644 --- a/framework/yii/base/Model.php +++ b/framework/yii/base/Model.php @@ -249,9 +249,9 @@ class Model extends Component implements IteratorAggregate, ArrayAccess * You may override this method to change the default behavior. * @return array list of attribute names. */ - public static function attributes() + public function attributes() { - $class = new ReflectionClass(get_called_class()); + $class = new ReflectionClass($this); $names = []; foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { if (!$property->isStatic()) { diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 0b201a2..bb2016a 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -570,7 +570,7 @@ class ActiveRecord extends Model * The default implementation will return all column names of the table associated with this AR class. * @return array list of attribute names. */ - public static function attributes() + public function attributes() { return array_keys(static::getTableSchema()->columns); } @@ -1256,7 +1256,7 @@ class ActiveRecord extends Model public static function create($row) { $record = static::instantiate($row); - $columns = array_flip(static::attributes()); + $columns = array_flip($record->attributes()); foreach ($row as $name => $value) { if (isset($columns[$name])) { $record->_attributes[$name] = $value; diff --git a/framework/yii/validators/UniqueValidator.php b/framework/yii/validators/UniqueValidator.php index dedceb9..053f795 100644 --- a/framework/yii/validators/UniqueValidator.php +++ b/framework/yii/validators/UniqueValidator.php @@ -64,11 +64,6 @@ class UniqueValidator extends Validator $className = $this->className === null ? get_class($object) : $this->className; $attributeName = $this->attributeName === null ? $attribute : $this->attributeName; - $attributes = $className::attributes(); - if (!in_array($attributeName, $attributes)) { - throw new InvalidConfigException("'$className' does not have an attribute named '$attributeName'."); - } - $query = $className::find(); $query->where([$attributeName => $value]); diff --git a/tests/unit/data/ar/elasticsearch/Customer.php b/tests/unit/data/ar/elasticsearch/Customer.php index 7d00dd3..0c07d08 100644 --- a/tests/unit/data/ar/elasticsearch/Customer.php +++ b/tests/unit/data/ar/elasticsearch/Customer.php @@ -19,7 +19,7 @@ class Customer extends ActiveRecord public $status2; - public static function attributes() + public function attributes() { return ['name', 'email', 'address', 'status']; } diff --git a/tests/unit/data/ar/elasticsearch/Item.php b/tests/unit/data/ar/elasticsearch/Item.php index 319783c..7e09b0c 100644 --- a/tests/unit/data/ar/elasticsearch/Item.php +++ b/tests/unit/data/ar/elasticsearch/Item.php @@ -11,7 +11,7 @@ namespace yiiunit\data\ar\elasticsearch; */ class Item extends ActiveRecord { - public static function attributes() + public function attributes() { return ['name', 'category_id']; } diff --git a/tests/unit/data/ar/elasticsearch/Order.php b/tests/unit/data/ar/elasticsearch/Order.php index 2efeb07..088011c 100644 --- a/tests/unit/data/ar/elasticsearch/Order.php +++ b/tests/unit/data/ar/elasticsearch/Order.php @@ -12,7 +12,7 @@ namespace yiiunit\data\ar\elasticsearch; */ class Order extends ActiveRecord { - public static function attributes() + public function attributes() { return ['customer_id', 'create_time', 'total']; } diff --git a/tests/unit/data/ar/elasticsearch/OrderItem.php b/tests/unit/data/ar/elasticsearch/OrderItem.php index e31e8e3..a43d8b2 100644 --- a/tests/unit/data/ar/elasticsearch/OrderItem.php +++ b/tests/unit/data/ar/elasticsearch/OrderItem.php @@ -12,7 +12,7 @@ namespace yiiunit\data\ar\elasticsearch; */ class OrderItem extends ActiveRecord { - public static function attributes() + public function attributes() { return ['order_id', 'item_id', 'quantity', 'subtotal']; } diff --git a/tests/unit/data/ar/redis/Customer.php b/tests/unit/data/ar/redis/Customer.php index 2c9dd82..63143ff 100644 --- a/tests/unit/data/ar/redis/Customer.php +++ b/tests/unit/data/ar/redis/Customer.php @@ -11,7 +11,7 @@ class Customer extends ActiveRecord public $status2; - public static function attributes() + public function attributes() { return ['id', 'email', 'name', 'address', 'status']; } diff --git a/tests/unit/data/ar/redis/Item.php b/tests/unit/data/ar/redis/Item.php index 1163265..0bcb072 100644 --- a/tests/unit/data/ar/redis/Item.php +++ b/tests/unit/data/ar/redis/Item.php @@ -4,7 +4,7 @@ namespace yiiunit\data\ar\redis; class Item extends ActiveRecord { - public static function attributes() + public function attributes() { return ['id', 'name', 'category_id']; } diff --git a/tests/unit/data/ar/redis/Order.php b/tests/unit/data/ar/redis/Order.php index 0769ce2..ec5e816 100644 --- a/tests/unit/data/ar/redis/Order.php +++ b/tests/unit/data/ar/redis/Order.php @@ -4,7 +4,7 @@ namespace yiiunit\data\ar\redis; class Order extends ActiveRecord { - public static function attributes() + public function attributes() { return ['id', 'customer_id', 'create_time', 'total']; } diff --git a/tests/unit/data/ar/redis/OrderItem.php b/tests/unit/data/ar/redis/OrderItem.php index 38def6b..a4c82b9 100644 --- a/tests/unit/data/ar/redis/OrderItem.php +++ b/tests/unit/data/ar/redis/OrderItem.php @@ -2,8 +2,6 @@ namespace yiiunit\data\ar\redis; -use yii\redis\RecordSchema; - class OrderItem extends ActiveRecord { public static function primaryKey() @@ -11,7 +9,7 @@ class OrderItem extends ActiveRecord return ['order_id', 'item_id']; } - public static function attributes() + public function attributes() { return ['order_id', 'item_id', 'quantity', 'subtotal']; }