how to select special fields in query in cakephp 3

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

how to select special fields in query in cakephp 3

Amir Shabani
Hi chums :]

I am very new on cakephp.  I've asked this question in stackoverflow , but nobody could not help.
Well, I am here to help me.

I need to join 3 tables (movies, comments, users) in my cakephp 3 controller and select only some special fields. I tested different solutions. I surfed a lot, but I could not solve it. Actually I need to show the last 3 comments on member profile.

Below is controller :

$movietbl = TableRegistry::get('Services.Movies');
        $movies = $movietbl->find()
                    'Comments' => function($q) {
                        return $q->autoFields(false)
                                ->select([ 'id' , 'title' , 'comments']);
                    'Userscomments' => function($q) {
                        return $q->autoFields(false)
                                ->select(['lname', 'fname', 'id']);
                        ->where(['Movies.user_id' => $this->getUserId()])
                $this->set('movies', $movies);
                $this->set('userId', $this->getUserId());

below is MoviesTable

$this->belongsToMany('Userscomments', [
            'className' => 'Services.Users',
            'foreignKey' => 'movie_id',
            'targetForeignKey' => 'user_id',

but it returns all of fields. What should I do now?

Thanks in advance

Like Us on FaceBook
Find us on Twitter

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at
For more options, visit