3

我有以下关系用户(id,名称,电子邮件,...)电影(id,名称,...) users_watchlists(id,user_id,movie_id)

它是HABTM关系。

错误

错误:SQLSTATE [42000]:语法错误或访问冲突:1066 不是唯一的表/别名:'UsersWatchlist'

SQL 查询:选择UsersWatchlistid, UsersWatchlist. first_name, UsersWatchlist. last_name, UsersWatchlist. display_image, UsersWatchlist. email, UsersWatchlist. password, UsersWatchlist. birthday, UsersWatchlist. gender, UsersWatchlist. group_id, UsersWatchlist. banned, UsersWatchlist. created, UsersWatchlist. modified, UsersWatchlist. id, UsersWatchlist. first_name, UsersWatchlist. last_name, UsersWatchlist. display_image, UsersWatchlist. email, UsersWatchlist. password, UsersWatchlist. birthday, UsersWatchlist. gender, UsersWatchlist. group_id, UsersWatchlist. banned, UsersWatchlist. created, UsersWatchlist. modifiedreelstubsusers作为 UsersWatchlist加入reelstubsusersUsersWatchlist开(UsersWatchlist. movie_id= 4 和UsersWatchlistuser_id= UsersWatchlistid)

注意:如果要自定义此错误消息,请创建 app\View\Errors\pdo_error.ctp

用户模型

    public $hasAndBelongsToMany = array(
       'UsersWatchlist' => array(
            'className' => 'Movie',
            'joinTable' => 'users_watchlists',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'movie_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
  );

电影模特

public $hasAndBelongsToMany = array(
'UsersWatchlist' => array(
        'className' => 'User',
        'joinTable' => 'users_watchlists',
        'foreignKey' => 'movie_id',
        'associationForeignKey' => 'user_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

用户观察名单

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Movie' => array(
        'className' => 'Movie',
        'foreignKey' => 'movie_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

不知道为什么它试图从 UsersWatchlist 中获取名称等

4

1 回答 1

2

不知道为什么它试图从 UsersWatchlist 中获取名称等

因为您有一个名为关系数据表的模型,UsersWatchlist并且您将 User 和 Movie 之间的关系命名为相同的名称。

尝试更新这些名称:

用户模型

public $hasAndBelongsToMany = array(
   'Movie' => array(
   ...

电影模特

public $hasAndBelongsToMany = array(
   'User' => array(
   ...

这些键在 SQL 查询中用作别名,在这种情况下会失败。

顺便说一句,如果您users_watchlists仅使用数据表来维护用户和电影之间的链接(例如,如果您不存储有关关系的任何属性),您可能只是摆脱UsersWatchlist模型。

于 2012-04-30T07:48:56.620 回答