CakePHP 3. Container Selection

I have many, many relationships where TrainingPrograms can contain many exercises. They are linked using a link binding program.

I want to select specific 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 this:

   "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 in the exercise table, not the fields that I requested. Why is it that I am doing wrong?

+4
source share
1 answer

belongsToMany Query::autoFields() , fields. , (exercise_id) SELECT, ( , ).

. > \Cake\ORM\Association\BelongsToMany:: _ buildQuery()

, autoFields(), .

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

, . GitHub IRC.

+5

Source: https://habr.com/ru/post/1610655/


All Articles