【问题标题】:CakePHP multiple HABTM associations in containable findCakePHP 可包含查找中的多个 HABTM 关联
【发布时间】:2012-10-31 15:47:54
【问题描述】:

我在两个模型之间有 HABTM 关联,​​但只能找到返回一个级别。我可以通过其他关联返回多个级别,但我认为我必须在 HABTM 中遗漏一些东西。

控制器/SchedulesController.php

$this->Schedule->find('first', array(
  'contain' => array(
    'Association' => array(
      'Schedule'
    )
  )
));

模型/Schedule.php

public $actsAs = array('Containable');
public $hasAndBelongToMany = array(
  'Association'
);

Model/Association.php

public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
  'Schedule'
);

目前我只得到...

array(
  'Schedule' => array(
     ...
  ),
  'Association' => array(
    (int) 0 => array(
      ...
    'AssociationsSchedule' => array(
      ...
    )
  )
)

...但我想要时间表 -> 协会 -> 时间表

【问题讨论】:

  • 为什么要两次相同的数据?

标签: cakephp find associations has-and-belongs-to-many contain


【解决方案1】:

虽然 contains() 应该可以工作,但另一个选项是在 find 之前使用 recursive 选项,如下所示:

$this->Schedule->recursive = 3; //2 might work, but I think you need 3 levels
$this->Schedule->find('first');

还值得一提的是,here 也提出了类似的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2012-11-29
    • 2011-07-12
    • 2011-10-23
    相关资源
    最近更新 更多