【问题标题】:How to self join a many to many table in cakephp 3?如何在cakephp 3中自我加入多对多表?
【发布时间】:2015-06-18 02:39:31
【问题描述】:

我尝试自行加入我的表用户以获取父子关联,但没有成功

这是我在用户模型中的关联:

        $this->belongsToMany('Parents', [
            'className' => 'Users',
            'joinTable' => 'users_users',
            'foreignKey' => 'id',
            'targetForeignKey' => 'parent_id'
            ]);
        $this->belongsToMany('Childs', [
            'className' => 'Users',
            'joinTable' => 'users_users',
            'foreignKey' => 'id',
            'targetForeignKey' => 'child_id'
            ]);

我没有在 UsersUsers 模型中设置参数。

当我这样做时:

  $test = $this->Users->find()->where(['id =' => 65])->contain(['Parents']);

我得到了子实体,但父母属性为空,所以我做错了,但我不知道是什么。

感谢您的帮助:)

【问题讨论】:

    标签: model many-to-many cakephp-3.0 self-join


    【解决方案1】:

    我的外键是坏的,是这样的:

        $this->belongsToMany('Parents', [
            'className' => 'Users',
            'joinTable' => 'users_users',
            'foreignKey' => 'child_id',
            'targetForeignKey' => 'parent_id'
            ]);
        $this->belongsToMany('Childs', [
            'className' => 'Users',
            'joinTable' => 'users_users',
            'foreignKey' => 'parent_id',
            'targetForeignKey' => 'child_id'
            ]);
    

    问题解决了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-19
      • 2018-11-19
      • 1970-01-01
      • 2011-06-06
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多