CakePHP 3. Containable select

自古美人都是妖i 提交于 2019-12-10 17:33:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33003321/cakephp-3-containable-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!