0

here is my table association code:

class UserMastersTable extends Table {
  public function initialize(array $config) {
        parent::initialize($config); 
        $this->table('user_masters');     
        $this->hasOne('person_masters', [
            'className' => 'person_masters',
            'foreign_key'=>'user_master_id',
            'dependent' => true
        ]);
    }
}

when in controller i am using: $this->UserMasters->get($id); it results only user_masters table data.. so how can i also get Associated tables data??

Krunal Dave
  • 272
  • 6
  • 16

1 Answers1

2

Use contain()

Copy-Paste from the manual:

You should use contain() when you want to load the primary model, and its associated data. While contain() will let you apply additional conditions to the loaded associations, you cannot constrain the primary model based on the associations. For more details on the contain(), look at Eager Loading Associations.

// In a controller or table method.

// As an option to find()
$query = $articles->find('all', ['contain' => ['Authors', 'Comments']]);

// As a method on the query object
$query = $articles->find('all');
$query->contain(['Authors', 'Comments']);

Read the manual before jumping into trial and error driven development! If you would have done one of the tutorials in the manual before this would be clear. So do them now, they'll cover a lot more of the basics.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • for retreive associated data use 'contain' in query each time??is there any way so we can retrive associated data default with out using 'contain' each time?? can we define it globally in Model?? – Krunal Dave Oct 08 '15 at 13:37
  • Recursive was removed because it is *bad practice* to include assocs all time. They should be *only* included when *really* needed. However you can add the madness back in the beforeFind() callback, see -> http://book.cakephp.org/3.0/en/orm/table-objects.html#beforefind Would you mind flagging the answer as correct? Thanks. – floriank Oct 08 '15 at 13:39