ajt
ajt

Reputation: 662

display data from matching clause in cakephp3

How do i display data from a matching clause (subjects table id and name)as nothing seems to work. I get the data but I cant seem to display the subjects id or name? I have tried a few variations and I checked the cakephp3 docs which doesnt seem to say anything on this point.

the error is Trying to get property of non-object

//controller
 $tutorSubjects = $this->Tutors->getAllTutorSubjects($searchSubject);

          foreach( $tutorSubjects as $item2):
         //      
          //    debug($item2);
            debug($item2->Tutor->Subjects->name);  
            debug($item2->Tutors->Subjects->name);  
           debug($item2->Tutors->Subject->name);  
           debug($item2->tutor->subjects->name); 
           debug($item2->subjects->name);   
           debug($item2->Subjects->name); 
            debug($item2->subject->name);

          endforeach;

//data
object(App\Model\Entity\Tutor) {

    'id' => (int) 12,
    'last_name' => 'Towe',
    'first_name' => 'Andre',
    '_matchingData' => [
        'Subjects' => object(App\Model\Entity\Subject) {

            'id' => (int) 28,
            'name' => 'Physics: Year 11',
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Subjects'

        }
    ],

//model
 $query3 = $this->find()
            // ->contain(['Subjects'])
              ->select(['Tutors.id','Tutors.last_name','Tutors.first_name'])
               ->where(['Tutors.tutor_inactive'=>0 
                   ])
              ->order(['Tutors.first_name' => 'ASC'])
              ->hydrate(true);

      if ($subId>0){

          $query3->matching('Subjects', function ($q) use ($subId) {

                 return $q
                   ->select(['Subjects.id','Subjects.name'])
                  ->where(['Subjects.id' =>$subId]);
                });

         return $query3;
       }

Upvotes: 0

Views: 291

Answers (1)

Greg Schmidt
Greg Schmidt

Reputation: 5098

The data that you've included pretty clearly shows that there's a _matchingData property, and that it's an array. So, $item2->_matchingData['Subjects']->name would be the way to access it.

Upvotes: 1

Related Questions