【问题标题】:CakePHP 3 on view show the field from a belongTo association modelCakePHP 3 在视图中显示了来自 belongTo 关联模型的字段
【发布时间】:2015-09-12 22:24:04
【问题描述】:

假设我有以下关联架构:

Person => [
     hasMany => [
         Courses => [Person.id = Courses.person_id]
     ],
Courses => [
    belongTo => [
         Schools => [School.id = Courses.school_id]
    ]

当我通过mydomain/person/view/1 查看一个人时,我需要一个表格来显示那个人的课程。在此表中,每门课程都需要显示学校的名称。

所以我在我的控制器上尝试了以下操作:

public function view($id = null)
{
    $person = $this->Persons->get($id, [
        'contain' => [
            'Courses.Schools',
        ]
    ]);
    $this->set('persons', $test);
    $this->set('_serialize', ['person']);
}

我看到的是:

Person => [
    firstname => test,
    lastname => test,
    courses => [
        0 => [
            id => 1,
            shool_id => 1,
            person_id => 1,
        ]
    ]
]

虽然我在包含选项中使用了它,但数组中没有学校。所以我不能显示学校的名字。我做错什么了吗?有什么指导方针如何在视图上显示这些字段。

【问题讨论】:

    标签: php cakephp associations cakephp-3.0


    【解决方案1】:

    基本上我对此感到抱歉。这是由debugKit 引起的。 debugkit 仅在我提到的级别之前通过变量面板显示关联,但我使用了var_dump 并看到关联和相关字段已正确获取/加载。我信任 debugKit,我认为它们没有加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 2018-10-27
      • 1970-01-01
      相关资源
      最近更新 更多