jagguy
jagguy

Reputation: 183

retreiving fields from asscoaied models cakephp3

I dont understand from the docs why I cant retrieve selected fields from associated data. I have 2 many to many relationships in the contain which displays everything fine.

$query3 = $this->find()
              ->contain(['Subjects','AvailabilityForStudents'])
              ->select([
                  'Students.id','Students.first_name','Students.last_name','Students.address_billing','Students.address_lat','Students.address_long',
                  'Students.class_year','Students.address_street','Students.address_suburb','Students.address_postcode','Students.address_state'])
               ->where(['Students.id IN' => $stids])
              ->order(['Students.first_name' => 'ASC'])
              ->hydrate(true);

My Question is that instead of getting every field from the contained models why cant I select fields from the contained model as below? I am not selecting based on a criteria like in matching clause?

   $query3 = $this->find()
              ->contain(['Subjects','AvailabilityForStudents'])
              ->select(['Subjects.name','Subjects.id','AvailabilityForStudents.id',
                  'Students.id','Students.first_name','Students.last_name','Students.address_billing','Students.address_lat','Students.address_long',
                  'Students.class_year','Students.address_street','Students.address_suburb','Students.address_postcode','Students.address_state'])
               ->where(['Students.id IN' => $stids])
              ->order(['Students.first_name' => 'ASC'])
              ->hydrate(true);

//student model

 $this->hasMany('AvailabilityForStudents', [
            'className' => 'AvailabilityForStudents',
            'foreignKey' => 'student_id',
            'dependent' => false,
        ]);

  $this->belongsToMany('Subjects', [
            'foreignKey' => 'student_id',
            'targetForeignKey' => 'subject_id',
            'joinTable' => 'subjects_students'
        ]);

      //update contain
 $query3 = $this->find()
                   ->contain([

                              'Subjects'=>['fields'=>['name','id','SubjectsStudents.student_id']],
                              'AvailabilityForStudents'  ])
              //   ->contain(['Subjects','AvailabilityForStudents'])
                 ->where(['Students.student_unallocated' =>  1,'Students.student_inactive' =>  0])

              ->order(['Students.first_name' => 'ASC'])
              ->hydrate(true);

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

Upvotes: 1

Views: 53

Answers (1)

bikash.bilz
bikash.bilz

Reputation: 821

You are doing right. You can try this also,

 $query3 = $this->find()
                  ->contain([
                                'Subjects'=>['fields'=>['name','id','SubjectsStudents.student_id']],
                              'AvailabilityForStudents'=>['fields'=>'id']
                           ])
                  ->select(['Students.id','Students.first_name','Students.last_name','Students.address_billing','Students.address_lat','Students.address_long',
                      'Students.class_year','Students.address_street','Students.address_suburb','Students.address_postcode','Students.address_state'])
                   ->where(['Students.id IN' => $stids])
                  ->order(['Students.first_name' => 'ASC'])
                  ->hydrate(false)->toArray();

Upvotes: 2

Related Questions