Search code examples
phpcakephpright-joincakephp-3.2

Do not know how to retrieve information with RightJoin on CakePHP 3.2


  • CakePHP Version: 3.2
  • Platform and Target: Wamp, MySQq, Google Chrome

What I did

Right Join between two tables in one controller. I'm trying to retrieve the information and I can't

$query = $this->Schedules->find('all')
  ->rightJoin(['Sessions'=> 'sessions'], ['Sessions.id is not null'])
  ->select(['Sessions.specialist_id'])
  ->where(['Sessions.id ='=>$session])

foreach ($query as $sp) {
  if(!empty($sp)){
    $specialist=$sp->Sessions->specialist_id;

Expected Behavior

Get the specialist id

Actual Behavior

I can't get the specialist id

More info

1 I already tried with $specialist=$sp->specialist_id; and nothing happen
2 With debug ($sp);die(); this is what I got

object(App\Model\Entity\Schedule) {

'Sessions' => [
    'specialist_id' => 'a7f6d5b2-b0f3-4a55-b6ce-41d393e80d12'
],
'[new]' => false,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Schedules'

}


Solution

  • specialist_id belongs to Sessions, so you need to access it via that key, ie

    $sp->Sessions['specialist_id']
    

    or

    $sp['Sessions']['specialist_id']
    

    or even

    $sp->get('Sessions')['specialist_id']
    

    ... oooooor, once that feature makes it into the core

    $sp->get('Sessions.specialist_id')
    

    That's rather basic stuff, but maybe the docs could benefit from an example on how to access nested data, including such that is a mix of objects and arrays.

    Cookbook > Database Access & ORM > Entities > Accessing Entity Data

    On a side note, AFAICT $sp should never be empty, either there is a result, or there isn't.