-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Description
The ActiveRecord::getAttributeLabel() method is able to fetch labels from related models. The documentation for this method is unclear and the current behaviour of this method leads to inconsistency in its handling of labels for attributes of related model overridden in primary model depending on how deeply relations are nested.
For example:
class OrderItem extends \yii\db\ActiveRecord
{
public function attributeLabels()
{
return [
'order.id' => 'Order ID',
// Currently have to define the same label already defined in `Order` model
// to get correct label value of 'Customer name' for $orderItem->getAttributeLabel('order.customer.name').
// 'order.customer.name' => 'Customer name',
];
}
public function getOrder()
{
return $this->hasOne(Order::class, [/*...*/]);
}
}
class Order extends \yii\db\ActiveRecord
{
public function attributeLabels()
{
return [
'id' => 'ID',
'customer.name' => 'Customer name',
];
}
public function getCustomer()
{
return $this->hasOne(Customer::class, [/*...*/]);
}
}
class Customer extends \yii\db\ActiveRecord
{
public function attributeLabels()
{
return [
'name' => 'Name',
];
}
}
var_dump($orderItem->getAttributeLabel('order.id')); // Line (1)
// Returns 'Order ID' - overridden label from primary model `OrderItem` instead of 'ID' from related model `Order`.
var_dump($orderItem->getAttributeLabel('order.customer.name'); // Line (2)
// Returns 'Name' - from related model 'Customer' instead of overridden label 'Customer name' from primary model `Order`.Lets say ActiveRecord::getAttributeLabel() allows overridding in primary model labels for attributes of related model. Then for the example above:
- line (1) - ok;
- line (2) - BUG.
And vice versa, if such override is not allowed:
- line (1) - BUG;
- line (2) - ok.
Since having the ability to override the labels in primary model for all levels of nesting is actually useful (as illustrated by the example above) the fact it does not work for deeply nested relations should be treated as a bug and fixed.
Additional info
| Q | A |
|---|---|
| Yii version | 2.0.48.1 |