【问题标题】:Cakephp3 doesn't recognize the table with different name in deep associationsCakephp3 无法识别深度关联中具有不同名称的表
【发布时间】:2018-12-31 02:09:26
【问题描述】:

我创建了一个名为Delegates 的表,并创建了一个用户表和用户实体。而且我在UsersTable 中使用了$this->setTable('delegates');,以便能够从$this->Users; 访问代表(我只想说我已经创建了一个带有代表表的用户模型)

到目前为止一切都很好......

在我的应用程序中,我试图访问深层关联。这个查询一切都很好,但是当我 contain User 模型时,我得到 The Users association is not defined on Comments.

我可以确认关联设置正确。

...
// There are more associations up there. 
...
'Comments', [
        'className' => 'App\Model\Table\CommentsTable',
        'foreignKey' => 'delegate_assessment_criteria_id',
        'belongsTo' => [
            'Assessors' => [
                'className' => 'App\Model\Table\AssessorsTable',
                'foreignKey' => 'assessor_id',
            ],
            'Users' => [
                'className' => 'App\Model\Table\UsersTable',
                'foreignKey' => 'delegate_id',
            ]
        ]
    ]

这是深度关联。

...
// There are more associations up there. 
...
'Comments' => function($q) {
    return $q
        ->select([
            'id'
        ])
        ->order(['Comments.created DESC'])  
        ->contain([
            'Assessors' => function($q) {
                return $q
                    ->select([
                        'id'
                    ])
                    ->enableAutoFields();
            }
        ])   
        ->contain([                                                                           
            'Users' => function($q) {
                return $q
                    ->select([
                        'id',
                        'delegate_id'
                    ])
                    ->enableAutoFields();
            }
        ])   
        ->enableAutoFields();
}

2 注意事项:

  • 如果我在查询层次结构的最前面包含User 模型,我 可以访问User 字段,但在深度关联中,这不会 工作。
  • 如果我包含Delegates,它会起作用。

我认为 Cakephp 查询生成器有问题。

【问题讨论】:

  • 你的第一个代码块,你定义Comments关联的地方,这是哪里?因为看起来您正在尝试在另一个关联中定义用户关联,这根本不是它的工作方式。相反,用户与评论的关联必须发生在评论模型中,以便与评论相关联的 任何东西 获得下一个级别,并且您不必为每个模型重新定义它与评论相关联。希望这很清楚。
  • @GregSchmidt 谢谢,但这只是为了让读者了解我的表模型的结构。正如我提到的,我有 2 张桌子 UsersDelegates。我想将delegates 用作Users。所有关联均已在 (Table Models) 中设置。请再次阅读问题。我认为深层 ORM 关联存在问题。
  • 很多人,包括我自己,每天都在使用深度联想。所以我相信你的代码有问题。如果您向我们展示更多您的初始化函数,我们或许可以提供更具体的建议。

标签: cakephp cakephp-3.7


【解决方案1】:

好吧,我终于想通了。在我不知道为什么我忘记之前我已经做到了。可能是因为我有很深的联想,所以我沉迷其中。

深层关联包含与第一个包含产生冲突。如果我在深度关联中设置不同的propertyName,那么它就可以达到目的。

请记住,您必须在表模型上设置此关联。

'Comments', [
        'className' => 'App\Model\Table\CommentsTable',
        'foreignKey' => 'delegate_assessment_criteria_id',
        'belongsTo' => [
            'Assessors' => [
                'className' => 'App\Model\Table\AssessorsTable',
                'foreignKey' => 'assessor_id',
            ],
            'Delegates' => [   //  <= Here set the name of Assossiation you want to be shown in array
                'className' => 'App\Model\Table\UsersTable',
                'propertyName' => 'delegates',   // <=  Add propertyName and set another name here
                'foreignKey' => 'delegate_id',
            ]
        ]
    ]

关于关联

'Comments' => function($q) {
    return $q
        ->select([
            'id'
        ])
        ->order(['Comments.created DESC'])  
        ->contain([
            'Assessors' => function($q) {
                return $q
                    ->select([
                        'id'
                    ])
                    ->enableAutoFields();
            }
        ])   
        ->contain([                                                                           
            'Users' => function($q) {
                return $q
                    ->select([
                        'id',
                        // 'delegate_id'  // <= Remove this - We have already done it when we set the association on above code.
                    ])
                    ->enableAutoFields();
            }
        ])   
        ->enableAutoFields();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2019-04-12
    相关资源
    最近更新 更多