1

我知道解决方案很简单,可能与 Containable 行为有关,但我无法让它发挥作用。没有所有的尝试

情况就是这样。我想显示事件的事件详细信息(例如会议)。每个活动都在一个 EventVenue 中进行,每个 EventVenue 都位于一个国家/地区。

因此,在国家模型中存在以下内容:

public $hasMany = array(
    'EventVenue' => array(
        'className' => 'EventVenue',
        'foreignKey' => 'country_id'
))

在 EventVenue 模型中,建立了 BelongsTo 关联

public $belongsTo = array(
    'Country' => array(
        'className' => 'Country',
        'foreignKey' => 'country_id'
))

并且在 Event 模型中建立了一个 hasOne 关联

 public $hasOne = array(
    'EventVenue' => array(
        'className' => 'EventVenue',
        'foreignKey' => 'event_id',
 ))

我想要的是在事件控制器中呈现的页面上显示国家名称。我确实获得了所有 Event 和 EventVenue 数据,但未检索到场地的相关国家/地区。

通过以下方式检索数据

 $item = $this->Event->findBySlug($slug);

我怎样才能从数据库中检索到国家名称(例如荷兰)?我尝试了这样的事情,但没有奏效:

 $item = $this->Event->findBySlug($slug,array(
    'contain' => array(
        'Event' => array(
            'EventVenue' => array(
                'Country'
            )
        )
    )
 )
4

3 回答 3

0

尝试这个:

$item = $this->Event->findBySlug($slug,array(
    'contain' => array(
        'EventVenue' => array(
            'Country'
        )
    )
);

更新

原来 findBy 不支持 Containable。您可以使用它来获得所需的结果:

$item = $this->Event->find('first',array(
    'conditions' => array(
        'Event.slug' => $slug
    ),
    'contain' => array(
        'EventVenue' => array(
            'Country'
        )
    )
);

哦,确保你在模型中有这个:public $actsAs = array('Containable');

于 2012-07-11T01:36:43.793 回答
0

在进行查找之前,您需要将递归设置为 2,如下所示

$this->Event->recursive = 2;

有了这个,您将一举获得 Event、EventVenue 和 Country

希望能帮助到你

于 2012-07-11T23:40:17.610 回答
0

试试这个方法:

$this->Event->recusive = 2;
$item = $this->Event->findBySlug($slug);
于 2012-07-11T10:01:51.523 回答