[英]CakePhp 3 Not unique table/alias: TableName
I've bin searching for days now without fixing this problem: 我现在要搜索几天,但没有解决此问题:
I am getting this error message: 我收到此错误消息:
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Posts'
And this is my initialize method in the PostTable class: 这是我在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'
]);
}
And this is my database: 这是我的数据库:
i know the error has to do something with the foreignkeys of the posts
table, but i don't know what is wrong with the belongTo
in my initialize method. 我知道该错误与
posts
表的外键有关,但我不知道我的initialize方法中的belongTo
有什么问题。
Since you have self association you need to use a different alias for that association. 由于您具有自我关联,因此需要为该关联使用其他别名。 So instead of
所以代替
$this->belongsTo('Posts', [
'foreignKey' => 'post_id',
'joinType' => 'INNER'
]);
use 采用
$this->belongsTo('ParentPosts', [
'className' => 'Posts',
'foreignKey' => 'post_id',
'joinType' => 'INNER'
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.