簡體   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