0
  • 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'

}

teph
  • 23
  • 4
  • 2
    "_I want X, but I don't get X_" is pretty much like "_It doesn't work_", which isn't really a proper problem description. Please be more specific as to what _exactly_ happens! Like, does the query retrieve the data that you want? Does the code actually make it to/past the `if`? If so, what exactly is `$specialist`? Also helpful would be to know what your datastructure (the involved schemas and the records) and the issued queries look like (and whether they look like what you'd expect). – ndm Jun 18 '16 at 11:39
  • Start with `debug($sp); die;` Do you really want a right join (find all sessions matching conditions, whether they have a schedule or not)? – AD7six Jun 18 '16 at 15:28
  • What I got with `debug($sp); die;` is now under the code @AD7six – teph Jun 18 '16 at 18:24
  • The specialist has sessions and the session, one schedule. What I'm trying to do is to validate that if I try to insert one schedule (with a start and an end datetime) and it collide with another schedule (of the same specialist), I must see the information according to the schedule that is already saved. First I need the specialist, so I made that query and yes, it retrieves what I want and past the if statement. If I print $sp, I saw on the screen {"Sessions":{"specialist_id": ->the id that I need<-}} . The problem is that I don't know how to retrieve just the id from $sp. @ndm – teph Jun 18 '16 at 18:24
  • I totally overlooked that `specialist_id` belongs to `Sessions`, and not to `Schedules` :) – ndm Jun 18 '16 at 19:26

1 Answers1

1

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.

ndm
  • 59,784
  • 9
  • 71
  • 110