Search code examples
cakephpormcakephp-3.0contain

CakePHP 3. Containable select


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?


Solution

  • 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.