CakePHP 3.可容纳选择 [英] CakePHP 3. Containable select

查看:248
本文介绍了CakePHP 3.可容纳选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多对多的关系,其中TrainingPrograms可以包含许多练习。



我想从练习中选择某些字段:

  $ trainingPrograms = $ this-> TrainingPrograms-> find()
- > contains(['Exercises'=> function($ q){
return $ q
- > select(['id','name','description']);
}
])
- > select ,'name','description'])
- > where(['user_id'=> $ this-> Auth-> user('id'

结果我看起来像这样:

 trainingPrograms:[
{
id:1,
name:Monday Madness,
description :hes,
exercises:[
{
id:2,
name:Barbell Bench Press,
description :Medium grip,
exercise_categories_id:2,
exercise_difficulties_id:1,
created:2015-09-16T07:07:01 + 0000 b $ bmodified:2015-09-16T07:07:01 + 0000,
_joinData:{
exercise_id:2,
id:28,
training_program_id:1,
created:2015-10-07T15:45:49 + 0000
}
},
{
id:2,
name:Barbell Bench Press,
description:Medium grip,
exercise_categories_id:2,
exercise_difficulties_id :1,
created:2015-09-16T07:07:01 + 0000,
modified:2015-09-16T07:07:01 + 0000 $ b_joinData:{
exercise_id:2,
id:35,
training_program_id:1,
created: 07T19:58:12 + 0000
}
}
]
}]

$ b b

正如你可以看到,我得到我的练习表的所有字段,而不是我要求的字段。为什么呢,我做错了什么?

解决方案

belongsToMany :autoFields(),以防未通过字段选项定义任何字段。这是必要的,因为外键( exercise_id )被添加到 SELECT 子句中,否则不会导致其他



请参阅 源> \Cake\ORM\Association\BelongsToMany :: _ buildQuery() p>

所包含的关联的回调在稍后被调用,因此您必须禁用 autoFields()

   - >包含(['Exercises'=> ; function($ q){
return $ q
- > select(['id','name','description'])
- > autoFields(false);
}



我不知道这是否是预期的行为。请在 在GitHub 上解决问题,或在IRC上询问。


I have a many to many relation where TrainingPrograms can contain many Exercises. They are a linked via the linktable ExercisesTrainingPrograms.

I want to select certain fields from my exercises:

$trainingPrograms = $this->TrainingPrograms->find()
            ->contain(['Exercises' => function ($q) {
                return $q
                    ->select(['id','name','description']);
            }
            ])
            ->select(['id','name','description'])
            ->where(['user_id' => $this->Auth->user('id')]);

The result i get looks like so:

   "trainingPrograms": [
            {
                "id": 1,
                "name": "Monday Madness",
                "description": "hes",
                "exercises": [
                    {
                        "id": 2,
                        "name": "Barbell Bench Press",
                        "description": "Medium grip ",
                        "exercise_categories_id": 2,
                        "exercise_difficulties_id": 1,
                        "created": "2015-09-16T07:07:01+0000",
                        "modified": "2015-09-16T07:07:01+0000",
                        "_joinData": {
                            "exercise_id": 2,
                            "id": 28,
                            "training_program_id": 1,
                            "created": "2015-10-07T15:45:49+0000"
                        }
                    },
                    {
                        "id": 2,
                        "name": "Barbell Bench Press",
                        "description": "Medium grip ",
                        "exercise_categories_id": 2,
                        "exercise_difficulties_id": 1,
                        "created": "2015-09-16T07:07:01+0000",
                        "modified": "2015-09-16T07:07:01+0000",
                        "_joinData": {
                            "exercise_id": 2,
                            "id": 35,
                            "training_program_id": 1,
                            "created": "2015-10-07T19:58:12+0000"
                        }
                    }
                ]
            }]

As you can see i get all the fields of my exercises table, rather than the fields that i asked for. Why is that, what am I doing wrong?

解决方案

belongsToMany associations do enable Query::autoFields() in case no fields have been defined via the fields option. This is necessary as the foreign key (exercise_id) is being added to the SELECT clause, which would otherwise cause no other fields to be selected (not sure in which context this is actually required).

See Source > \Cake\ORM\Association\BelongsToMany::_buildQuery()

The callbacks for the contained associations are being invoked at a later point, so that you'll have to disable autoFields() in order to be able restrict the selected fields via the query builder.

->contain(['Exercises' => function ($q) {
    return $q
        ->select(['id','name','description'])
        ->autoFields(false);
}

I can't really tell whether this is the intended behavior. You may want to open an issue over at GitHub for clarification, or ask on IRC.

这篇关于CakePHP 3.可容纳选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆