PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] CakePHP 3. Containable select

 January 17, 2022     cakephp, cakephp-3.0, contain, orm     No comments   

Issue

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.



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing