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
* 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.');
}
......
......@@ -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.');
}
......
......@@ -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);
}
......
......@@ -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()) {
......
......@@ -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;
......
......@@ -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]);
......
......@@ -19,7 +19,7 @@ class Customer extends ActiveRecord
public $status2;
public static function attributes()
public function attributes()
{
return ['name', 'email', 'address', 'status'];
}
......
......@@ -11,7 +11,7 @@ namespace yiiunit\data\ar\elasticsearch;
*/
class Item extends ActiveRecord
{
public static function attributes()
public function attributes()
{
return ['name', 'category_id'];
}
......
......@@ -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'];
}
......
......@@ -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'];
}
......
......@@ -11,7 +11,7 @@ class Customer extends ActiveRecord
public $status2;
public static function attributes()
public function attributes()
{
return ['id', 'email', 'name', 'address', 'status'];
}
......
......@@ -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'];
}
......
......@@ -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'];
}
......
......@@ -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'];
}
......
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