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;
Get the specialist id
I can't get the specialist id
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'
}
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.