dsio68964998 2015-11-14 14:34
浏览 90

CakePhp 3不唯一的表/别名: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:

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:

Database ERD

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.

  • 写回答

1条回答 默认 最新

  • drpjdfj618393 2015-11-14 14:48
    关注

    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'
    ]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表