php - Join query retrieving data from only one table cakephp 3 -
i new cakephp. usign query builder fetch details 2 tables using join in cakephp query builder.but query writing fetching details 1 table. need data other table well.
this code fetch data joining 2 tables:
public function edit($id = null) { $events_table = tableregistry::get('events'); $events = $events_table->find('all') ->hydrate(false) ->join([ 'coursetype'=> [ 'table' => 'coursetype', 'type' => 'left', 'conditions' => 'events.type = coursetype.coursetype_id', ] ])->where(['events.id' => $id]); $event = $events->toarray(); $this->set('event', $event); }
as result getting details events table. need data coursetype also.any appreciated. thank you.
manually adding joins won't cause additional data selected, you'd have on own specifying fields selected via select()
method.
$events = $events_table ->find('all') ->hydrate(false) ->select($events_table) // either add fields 1 one, or pass table object // above select fields of table ->select(['coursetype.id', 'coursetype.xyz', /* ... */]) // ...
i'd suggest use use containments instead, ie setup required association if not present, , use contain()
:
$events = $events_table ->find('all') ->hydrate(false) ->contain('coursetype') ->where(['events.id' => $id]);
see also
Comments
Post a Comment