在 DetailView yii2 中顯示 HAS_MANY 關係數據 (Display HAS_MANY relation data in DetailView yii2)


問題描述

在 DetailView yii2 中顯示 HAS_MANY 關係數據 (Display HAS_MANY relation data in DetailView yii2)

我想知道是否可以在 detailview 中顯示一對多的關係數據。例如,用戶可以註冊多個電子郵件。那麼是否可以在 detailview 中顯示一個用戶註冊的所有電子郵件。

我嘗試過類似下面的方法,但它不起作用。

<?= DetailView::widget([
    'model'      => $model,
    'attributes' => [
        'id',
        'username',
        'firstname',
        'lastname',
        'location',
        'is_active:boolean',
        [
            'label' => 'Emails',
            'value' => ArrayHelper::map(
                UserEmails::find()
                    ‑>where(['user_id'=>$model‑>id])
                    ‑>orderBy('id')
                    ‑>asArray()
                    ‑>all(),
                'id', 'email')
        ],
    ],
]) ?>

我無論如何我都會出錯,但我想知道這可能嗎?我在這裡做錯了什麼??


參考解法

方法 1:

In DetailView, item's value can be only a value. So you can prepopulate that value.

$email = implode(',',UserEmails::find()‑>select('email')‑>where(['user_id'=>$model‑>id])‑>orderBy('id')‑>asArray()‑>all())
    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'username',
            'firstname',
            'lastname',
            'location',
            'is_active:boolean',
            [
                'label' => 'Emails',
                'value' => $emails
            ],

        ],
    ]) ?>

方法 2:

If your model contains hasMany() function, for example getCategories() or anything, you can just specify the very minimalist code to your DetailView. Also, I am using a clojure.

[
    'attribute' => 'categories',
    'value' => function ($model)
    {
        return implode(', ', $model‑>getCategories()‑>select('label')‑>column());
    }
],

(by Mike RossFabrizio CaldarelliPrabowo Murti)

參考文件

  1. Display HAS_MANY relation data in DetailView yii2 (CC BY‑SA 2.5/3.0/4.0)

#has-many #yii2 #detailsview






相關問題

Ruby on Rails - Has_many,批量分配問題 (Ruby on Rails - Has_many, Mass Assignment issue)

如何在 hasMany 關聯中使用 CakePHP 2 查找? (How to use CakePHP 2 find in a hasMany association?)

模型沒有像我認為的那樣連接 (Models not connecting as I think they should be)

CakePhp find không tìm nạp hasMany mối quan hệ (CakePhp find doesn't fetch hasMany relationships)

創建關聯後,控制器中的 Ruby on rails NoMethodError (Ruby on rails NoMethodError in controller after create an association)

是否可以以兩種不同的方式關聯模型? (Is it possible to associate models in two different ways?)

在 DetailView yii2 中顯示 HAS_MANY 關係數據 (Display HAS_MANY relation data in DetailView yii2)

Rails - 檢查 has_many 關聯中是否存在記錄 (Rails - check if record exists in has_many association)

在 Rails 中,我可以在另一個模型的模型上設置 has_many 關係嗎? (In Rails, can I set a has_many relation on a model from another model?)

從大於 0 開始計數器緩存 (Starting a counter cache from greater than 0)

如何使用 has_many 關聯在 Rails 中排序? (How to sort in rails with has_many association?)

如何將 ActiveModel::Serializer :has_many 關聯的密鑰格式(即駝峰式)指定為一次性選項(不是全局配置)? (How to specify the key format (i.e. camelcase) for an ActiveModel::Serializer :has_many association as a one-off option (not global config)?)







留言討論