Yii2如何在attributeLabels函数中使用具有相同值的多个数组键?

huangapple go评论141阅读模式
英文:

Yii2 How do I use multiple array keys with the same value inside attributeLabels function

问题

我正在创建一个表格内的列,该列可以具有0、1、2或3。所有这些数字都有特定的值,例如0 = 狗,1 = 猫,2 = 鱼,3 = 青蛙。为了在表格内获取名称而不是数字,我执行以下操作:

public function attributeLabels(): array
{
    return [
        Pet::DOG => '狗',
        Pet::CAT => '猫',
        Pet::FISH => '鱼',
        Pet::FROG => '青蛙',
        Pet::TYPE_LAND => '陆地',
        Pet::TYPE_WATER => '水生'
    ];
}

在Gridview中:

[
    'attribute' => 'Animal',
    'header' => Icon::show('beast', ['title' => Yii::t('backend', 'animal')]),
    'value' => function ($model) use ($petSearch) {
         return $petSearch->getAttributeLabel($model['Animal']);
     },
     'filter' => [
         Pet::DOG => $PetSearch->getAttributeLabel(Pet::DOG),
         Pet::CAT => $PetSearch->getAttributeLabel(Pet::CAT),
         Pet::FISH => $PetSearch->getAttributeLabel(Pet::FISH),
         Pet::FROG => $PetSearch->getAttributeLabel(Pet::FROG),
     ]
],

这应该会显示正确的翻译列,但存在一个问题。由于TYPE_LAND和TYPE_WATER也具有值0和1,因此它给出了混合的结果,因此该列不合理,例如,我看到的是Land而不是狗。我还在代码中收到以下消息:

具有值 '0' 的重复数组键

有没有办法使这个工作正常?

英文:

I am creating a column inside a table that can have 0, 1, 2 or 3. All these numbers have a certain value, in my case 0 = DOG, 1 = CAT, 2 = FISH, 3 = FROG. To get the names inside the table instead of the numbers I do the following:

public function attributeLabels(): array
{
    return [
        Pet::DOG => 'Dog',
        Pet::CAT => 'CAT',
        Pet::FISH => 'Fish',
        Pet::FROG => 'Frog',
        Pet::TYPE_LAND => 'Land',
        Pet::TYPE_WATER => 'Water'
}

And in the Gridview

[
    'attribute' => 'Animal',
    'header' => Icon::show('beast', ['title' => Yii::t('backend', 'animal')]),
    'value' => function ($model) use ($petSearch) {
         return $petSearch->getAttributeLabel($model['Animal']);
     },
     'filter' => [
         Pet::DOG => $PetSearch->getAttributeLabel(Pet::DOG),
         Pet::CAT => $PetSearch->getAttributeLabel(Pet::CAT),
         Pet::FISH => $PetSearch->getAttributeLabel(Pet::FISH),
         Pet::FROG => $PetSearch->getAttributeLabel(Pet::FROG),
     ]
],

This should show me the column with the right translations and it does, but there is a problem. Sinds TYPE_LAND and TYPE_WATER also have the values 0 and 1 it gives me a mix of the 2 so the column makes no sense for instance instead of dog I see Land. I also get the following message in my code:

Duplicate array key with value '0' 

Is there a way to make this work correctly

答案1

得分: 1

你不需要使用 attributeLabels() 方法来实现这个功能。该方法的作用是为模型的属性添加标签,而不是为属性的值添加标签。

你可以在你的 PetSearch 模型中创建两个公共方法,用于将值翻译为正确的标签,例如:

public function animalLabel($animal) {
    $map = [
        Pet::DOG => '狗',
        Pet::CAT => '猫',
        Pet::FISH => '鱼',
        Pet::FROG => '青蛙',
    ];

    if (isset($map[$animal])) {
        return $map[$animal];
    }
    // ... 如果值不在映射中,可以执行一些操作,例如抛出异常,返回空/默认标签等...
}

public function animalTypeLabel($type) {
    $map = [
        Pet::TYPE_LAND => '陆地',
        Pet::TYPE_WATER => '水中',
    ];

    if (isset($map[$type])) {
        return $map[$type];
    }
    // ... 如果值不在映射中,可以执行一些操作,例如抛出异常,返回空/默认标签等...
}

然后,在你的 GridView 配置中使用这些方法来替换数值为标签:

[
    'attribute' => 'Animal',
    'header' => Icon::show('beast', ['title' => Yii::t('backend', '动物')]),
    'format' => function ($value) use ($petSearch) {
        return $petSearch->animalLabel($value);
    },
    'filter' => [
        Pet::DOG => $petSearch->animalLabel(Pet::DOG),
        Pet::CAT => $petSearch->animalLabel(Pet::CAT),
        Pet::FISH => $petSearch->animalLabel(Pet::FISH),
        Pet::FROG => $petSearch->animalLabel(Pet::FROG),
    ],
],
[
    // 假设类型值在 `AnimalType` 属性中 - 请根据你的属性名称进行更改
    'attribute' => 'AnimalType',
    'header' => Icon::show('beast', ['title' => Yii::t('backend', '动物类型')]),
    'format' => function ($value) use ($petSearch) {
        return $petSearch->animalTypeLabel($value);
    },
    'filter' => [
        Pet::TYPE_LAND => $petSearch->animalTypeLabel(Pet::TYPE_LAND),
        Pet::TYPE_WATER => $petSearch->animalTypeLabel(Pet::TYPE_WATER),
    ],
],
英文:

You don't have to use attributeLabels() method for this. That method is supposed to label model's attributes not its values.

You can create two public methods for translating values into proper labels in your PetSearch model for example like this:

public function animalLabel($animal) {
    $map = [
        Pet::DOG => 'Dog',
        Pet::CAT => 'CAT',
        Pet::FISH => 'Fish',
        Pet::FROG => 'Frog',
    ];

    if (isset($map[$animal])) {
        return $map[$animal];
    }
    // ... do something if the value is not present in map
    // for example throw an exception, return empty/default label or something...
}

public function animalTypeLabel($type) {
    $map = [
        Pet::TYPE_LAND => 'Land',
        Pet::TYPE_WATER => 'Water'
    ];

    if (isset($map[$type])) {
        return $map[$type];
    }
    // ... do something if the value is not present in map
    // for example throw an exception, return empty/default label or something...
}

Then in your GridView config use those methods to replace numeric value with label:

[
    'attribute' => 'Animal',
    'header' => Icon::show('beast', ['title' => Yii::t('backend', 'animal')]),
    'format' => function ($value) use ($petSearch) {
         return $petSearch->animalLabel($value);
    },
    'filter' => [
        Pet::DOG => $petSearch->animalLabel(Pet::DOG),
        Pet::CAT => $petSearch->animalLabel(Pet::CAT),
        Pet::FISH => $petSearch->animalLabel(Pet::FISH),
        Pet::FROG => $petSearch->animalLabel(Pet::FROG),
    ],
],
[
    // asuming the type value is in `AnimalType` attribute - change to match your attribute names
    'attribute' => 'AnimalType',
    'header' => Icon::show('beast', ['title' => Yii::t('backend', 'animal')]),
    'format' => function ($value) use ($petSearch) {
         return $petSearch->animalTypeLabel($value);
     },
    'filter' => [
        Pet::TYPE_LAND => $petSearch->animalTypeLabel(Pet::TYPE_LAND),
        Pet::TYPE_WATER => $petSearch->animalTypeLabel(Pet::TYPE_WATER),
    ],
],

huangapple
  • 本文由 发表于 2023年8月10日 17:29:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定