【问题标题】:CakePHP 3.x getting accosiated data from entityCakePHP 3.x 从实体获取相关数据
【发布时间】:2016-03-23 18:33:37
【问题描述】:

我开始使用 CakePHP 3.x 并且想知道如何以正确的方式实现这些功能?

$articles = $userEntity->getAccosiatedArticles()

【问题讨论】:

    标签: php orm cakephp-3.0


    【解决方案1】:

    只需使用contain 来检索关联数据。

    $articles = $userEntity->find('all',[
                'contain'=>['Articles']
            ]);
    

    为此,您必须在用户模型中声明has_many(在这种情况下)关联。

    $this->hasMany('Articles');
    

    【讨论】:

      【解决方案2】:

      正确的方法是使用$usersTable->loadInto($userEntity, ['Articles'])。你也可以使用惰性加载器插件:https://github.com/jeremyharris/cakephp-lazyload

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        相关资源
        最近更新 更多