Bootstrap 如何处理Yii2上Collapse::widget中的asMany关系

qni6mghb  于 2022-12-07  发布在  Bootstrap
关注(0)|答案(1)|浏览(147)

我在我的系统上制作了一个小票模块yii 2,这些票是由笔记组成的,我想在其中汇总每一张票,我想用Collapse小部件yii 2(http:yiiframework.com)来显示相关笔记的列表,但我发现以下错误:正在尝试获取非对象的属性

型号

  • 门票 *
public function rules()
{
    return [
        [['content','subject'], 'string'],
    ];
}

public function attributeLabels()
{
    return [
        'id_ticket' => Yii::t('app', 'Id Ticket'),
        'subject' => Yii::t('app', 'Subject'),
        'content' => Yii::t('app', 'Content'),
    ];
}

public function getNotes()
{
    return $this->hasMany(Nota::className(), ['ticket_id' => 'id_ticket']);
}
  • 备注 *
public function rules()
{
    return [
        [['content','subject'], 'string'],
    ];
}

public function attributeLabels()
{
    return [
        'id_note' => Yii::t('app', 'Id Note'),
        'ticket_id' => Yii::t('app', 'Ticket ID'),
        'subject' => Yii::t('app', 'subject'),
        'content' => Yii::t('app', 'content'),
    ];
}

public function getTicket()
{
    return $this->hasOne(Ticket::className(), ['id_ticket' => 'ticket_id']);
}

检视

Collapse::widget([
                   'items' => [
                        [
                        'label' => $model->notes->subject,
                        'content' => $model->notes->content,
                        ],
                    ]
                 ]);
icomxhvb

icomxhvb1#

试着重复注解:

$notes= [];

foreach($model->notes as $note) {
  $notes[] = [
    'label' => $note->subject,
    'content' => $note->content,
  ];
}

然后将注解分配给小部件:

Collapse::widget(['items' => $nodes]);

希望这对你有帮助:)

相关问题