Thursday, December 17, 2015

how to select special fields in query in cakephp 3

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()                  ->contain([                      '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()])                          ->limit(3);                  $this->set('movies', $movies);                  $this->set('userId', $this->getUserId());


below is MoviesTable

$this->belongsToMany('Userscomments', [              'className' => 'Services.Users',              'joinTable'=>'comments',              'foreignKey' => 'movie_id',              'targetForeignKey' => 'user_id',          ]);      }


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

Thanks in advance

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: