0
votes

I'm new to cakePHP and I'm having issues getting all of my models to link up correctly.

This is the layout of my models in cakePHP:

Class belongsTo Location
Class belongsTo ClassType
Class hasMany ScheduledClass
ScheduledClass belongsTo Instructor

My problem is that when I use:

$this->Class->find('all')

I only get data from the Class, Location, ClassType and ScheduledClass models. I do not get any data from the Instructor model.

I can set the recursive value to '2' and retreive the data from the Instructor model, but this results in a giant amount of queries(one for every row) instead of a join on "ScheduledClass.instructor_id = Instructor.id".

What I was hoping to acheive is something like:

SELECT
...
...
FROM classes as Class
INNER JOIN locations as Location on Class.location_id = Location.id
INNER JOIN class_types as ClassType on Class.class_type_id = ClassType.id
INNER JOIN scheduled_classes as ScheduledClass on ScheduledClass.class_id = Class.id
INNER JOIN instructors as Instructor on ScheduledClass.instructor_id = Instructor.id

I've tried using both Containable and Joins in order to get the right data, but I wasn't able to get either to work(possibly due to my misunderstanding their uses).

Thank you in advance!

1

1 Answers

0
votes

use Containable and set how you want the find to react and fetch what data you want.

in the Model

var $actsAs = array('Containable');

and in the Controller you can do something like this

$this->data = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')), 'contain' => array('City', 'Region', 'Country', 'UserOccupations', 'UserGroup')));

if you have your Model/Databases relationships set right you can pull the exact data you want even choosing which columns you wanna pull.