烘焙HABTM关联

时间:2013-06-20 19:59:18

标签: cakephp has-and-belongs-to-many cakephp-bake

我收到以下错误,无法理解原因。我可以看到PodcastsUser不是一个表...我用下面的DB烘焙了整个应用程序 - 我正好在访问/Podcasts/view/*Nr*index等时收到错误。谢谢!

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'PodcastsUser.podcast_id' in 'on clause'

SQL查询:

SELECT `User`.`id`, `User`.`email`, `User`.`password`, `PodcastsUser`.`users_id`, `PodcastsUser`.`podcasts_id` 
FROM `podcaster`.`users` 
AS `User` 
JOIN `podcaster`.`podcasts_users` 
AS `PodcastsUser` 
ON (`PodcastsUser`.`podcast_id` = 1 
AND `PodcastsUser`.`user_id` = `User`.`id`)

这是我的HABTM协会: enter image description here

模型/ user.php的

public $hasAndBelongsToMany = array(
    'Podcast' => array(
        'className' => 'Podcast',
        'joinTable' => 'podcasts_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'podcast_id',
        'unique' => 'keepExisting',
    )
);

模型/ Podcast.php

    public $hasAndBelongsToMany = array(
    'User' => array(
        'className' => 'User',
        'joinTable' => 'podcasts_users',
        'foreignKey' => 'podcast_id',
        'associationForeignKey' => 'user_id',
        'unique' => 'keepExisting',
    )
);

--- --- EDIT PodcastsController view操作:

public function view($id = null) {
    if (!$this->Podcast->exists($id)) {
        throw new NotFoundException(__('Invalid podcast'));
    }
    $options = array('conditions' => array('Podcast.' . $this->Podcast->primaryKey => $id));
    $this->set('podcast', $this->Podcast->find('first', $options));
}

0 个答案:

没有答案