Commit 83527e85 by Carsten Brandt

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
parent e1fe5115
...@@ -216,7 +216,7 @@ class ActiveRecord extends \yii\db\ActiveRecord ...@@ -216,7 +216,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
* This method must be overridden by child classes to define available attributes. * This method must be overridden by child classes to define available attributes.
* @return array list of attribute names. * @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.'); throw new InvalidConfigException('The attributes() method of elasticsearch ActiveRecord has to be implemented by child classes.');
} }
......
...@@ -81,7 +81,7 @@ class ActiveRecord extends \yii\db\ActiveRecord ...@@ -81,7 +81,7 @@ class ActiveRecord extends \yii\db\ActiveRecord
* This method must be overridden by child classes to define available attributes. * This method must be overridden by child classes to define available attributes.
* @return array list of attribute names. * @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.'); throw new InvalidConfigException('The attributes() method of redis ActiveRecord has to be implemented by child classes.');
} }
......
...@@ -623,7 +623,7 @@ abstract class ActiveRecord extends Model ...@@ -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. * The default implementation will return all column names of the table associated with this AR class.
* @return array list of attribute names. * @return array list of attribute names.
*/ */
public static function attributes() public function attributes()
{ {
return array_keys(static::getIndexSchema()->columns); return array_keys(static::getIndexSchema()->columns);
} }
......
...@@ -249,9 +249,9 @@ class Model extends Component implements IteratorAggregate, ArrayAccess ...@@ -249,9 +249,9 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
* You may override this method to change the default behavior. * You may override this method to change the default behavior.
* @return array list of attribute names. * @return array list of attribute names.
*/ */
public static function attributes() public function attributes()
{ {
$class = new ReflectionClass(get_called_class()); $class = new ReflectionClass($this);
$names = []; $names = [];
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
if (!$property->isStatic()) { if (!$property->isStatic()) {
......
...@@ -570,7 +570,7 @@ class ActiveRecord extends Model ...@@ -570,7 +570,7 @@ class ActiveRecord extends Model
* The default implementation will return all column names of the table associated with this AR class. * The default implementation will return all column names of the table associated with this AR class.
* @return array list of attribute names. * @return array list of attribute names.
*/ */
public static function attributes() public function attributes()
{ {
return array_keys(static::getTableSchema()->columns); return array_keys(static::getTableSchema()->columns);
} }
...@@ -1256,7 +1256,7 @@ class ActiveRecord extends Model ...@@ -1256,7 +1256,7 @@ class ActiveRecord extends Model
public static function create($row) public static function create($row)
{ {
$record = static::instantiate($row); $record = static::instantiate($row);
$columns = array_flip(static::attributes()); $columns = array_flip($record->attributes());
foreach ($row as $name => $value) { foreach ($row as $name => $value) {
if (isset($columns[$name])) { if (isset($columns[$name])) {
$record->_attributes[$name] = $value; $record->_attributes[$name] = $value;
......
...@@ -64,11 +64,6 @@ class UniqueValidator extends Validator ...@@ -64,11 +64,6 @@ class UniqueValidator extends Validator
$className = $this->className === null ? get_class($object) : $this->className; $className = $this->className === null ? get_class($object) : $this->className;
$attributeName = $this->attributeName === null ? $attribute : $this->attributeName; $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 = $className::find();
$query->where([$attributeName => $value]); $query->where([$attributeName => $value]);
......
...@@ -19,7 +19,7 @@ class Customer extends ActiveRecord ...@@ -19,7 +19,7 @@ class Customer extends ActiveRecord
public $status2; public $status2;
public static function attributes() public function attributes()
{ {
return ['name', 'email', 'address', 'status']; return ['name', 'email', 'address', 'status'];
} }
......
...@@ -11,7 +11,7 @@ namespace yiiunit\data\ar\elasticsearch; ...@@ -11,7 +11,7 @@ namespace yiiunit\data\ar\elasticsearch;
*/ */
class Item extends ActiveRecord class Item extends ActiveRecord
{ {
public static function attributes() public function attributes()
{ {
return ['name', 'category_id']; return ['name', 'category_id'];
} }
......
...@@ -12,7 +12,7 @@ namespace yiiunit\data\ar\elasticsearch; ...@@ -12,7 +12,7 @@ namespace yiiunit\data\ar\elasticsearch;
*/ */
class Order extends ActiveRecord class Order extends ActiveRecord
{ {
public static function attributes() public function attributes()
{ {
return ['customer_id', 'create_time', 'total']; return ['customer_id', 'create_time', 'total'];
} }
......
...@@ -12,7 +12,7 @@ namespace yiiunit\data\ar\elasticsearch; ...@@ -12,7 +12,7 @@ namespace yiiunit\data\ar\elasticsearch;
*/ */
class OrderItem extends ActiveRecord class OrderItem extends ActiveRecord
{ {
public static function attributes() public function attributes()
{ {
return ['order_id', 'item_id', 'quantity', 'subtotal']; return ['order_id', 'item_id', 'quantity', 'subtotal'];
} }
......
...@@ -11,7 +11,7 @@ class Customer extends ActiveRecord ...@@ -11,7 +11,7 @@ class Customer extends ActiveRecord
public $status2; public $status2;
public static function attributes() public function attributes()
{ {
return ['id', 'email', 'name', 'address', 'status']; return ['id', 'email', 'name', 'address', 'status'];
} }
......
...@@ -4,7 +4,7 @@ namespace yiiunit\data\ar\redis; ...@@ -4,7 +4,7 @@ namespace yiiunit\data\ar\redis;
class Item extends ActiveRecord class Item extends ActiveRecord
{ {
public static function attributes() public function attributes()
{ {
return ['id', 'name', 'category_id']; return ['id', 'name', 'category_id'];
} }
......
...@@ -4,7 +4,7 @@ namespace yiiunit\data\ar\redis; ...@@ -4,7 +4,7 @@ namespace yiiunit\data\ar\redis;
class Order extends ActiveRecord class Order extends ActiveRecord
{ {
public static function attributes() public function attributes()
{ {
return ['id', 'customer_id', 'create_time', 'total']; return ['id', 'customer_id', 'create_time', 'total'];
} }
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
namespace yiiunit\data\ar\redis; namespace yiiunit\data\ar\redis;
use yii\redis\RecordSchema;
class OrderItem extends ActiveRecord class OrderItem extends ActiveRecord
{ {
public static function primaryKey() public static function primaryKey()
...@@ -11,7 +9,7 @@ class OrderItem extends ActiveRecord ...@@ -11,7 +9,7 @@ class OrderItem extends ActiveRecord
return ['order_id', 'item_id']; return ['order_id', 'item_id'];
} }
public static function attributes() public function attributes()
{ {
return ['order_id', 'item_id', 'quantity', 'subtotal']; return ['order_id', 'item_id', 'quantity', 'subtotal'];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment