繁体   English   中英

CakePhp 3不是唯一的表/别名:TableName

[英]CakePhp 3 Not unique table/alias: TableName

我现在要搜索几天,但没有解决此问题:

我收到此错误消息:
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Posts'

这是我在PostTable类中的initialize方法:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('posts');
    $this->displayField('post_id');
    $this->primaryKey('post_id');
    $this->addBehavior('timestamp');

    $this->belongsTo('Posts', [
        'foreignKey' => 'post_id',
        'joinType' => 'INNER'
    ]);

    $this->belongsTo('Users', [
        'className' => 'Users',
        'foreignKey' => 'fk_post_user_id', //<- foreignkey name are correct
        'joinType' => 'INNER'
    ]);

    $this->belongsTo('Pictures',[
        'foreignKey' => 'fk_post_picture_id',
        'joinType' => 'INNER'
    ]);
}

这是我的数据库:

数据库ERD

我知道该错误与posts表的外键有关,但我不知道我的initialize方法中的belongTo有什么问题。

由于您具有自我关联,因此需要为该关联使用其他别名。 所以代替

$this->belongsTo('Posts', [
    'foreignKey' => 'post_id',
    'joinType' => 'INNER'
]);

采用

$this->belongsTo('ParentPosts', [
    'className' => 'Posts',
    'foreignKey' => 'post_id',
    'joinType' => 'INNER'
]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM