Browse Source

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
tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
83527e85ca
  1. 2
      extensions/elasticsearch/ActiveRecord.php
  2. 2
      extensions/redis/ActiveRecord.php
  3. 2
      extensions/sphinx/ActiveRecord.php
  4. 4
      framework/yii/base/Model.php
  5. 4
      framework/yii/db/ActiveRecord.php
  6. 5
      framework/yii/validators/UniqueValidator.php
  7. 2
      tests/unit/data/ar/elasticsearch/Customer.php
  8. 2
      tests/unit/data/ar/elasticsearch/Item.php
  9. 2
      tests/unit/data/ar/elasticsearch/Order.php
  10. 2
      tests/unit/data/ar/elasticsearch/OrderItem.php
  11. 2
      tests/unit/data/ar/redis/Customer.php
  12. 2
      tests/unit/data/ar/redis/Item.php
  13. 2
      tests/unit/data/ar/redis/Order.php
  14. 4
      tests/unit/data/ar/redis/OrderItem.php

2
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.');
}

2
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.');
}

2
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);
}

4
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()) {

4
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;

5
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]);

2
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'];
}

2
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'];
}

2
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'];
}

2
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'];
}

2
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'];
}

2
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'];
}

2
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'];
}

4
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'];
}

Loading…
Cancel
Save