Wednesday, August 20, 2014

Get Titles for linked table in another relationship

I am very new to CakePHP and I am not sure the proper phrasing for my question when searching google, so I am asking here.

I have these tables:

TABLE `volunteers`  (This is actually Parent Volunteers for a school) (
  `ID` smallint(5) unsigned NOT NULL auto_increment,
  `FNAME` varchar(25) NOT NULL,
  `LNAME` varchar(25) NOT NULL,
  PRIMARY KEY  (`ID`)

TABLE `students` (
  `ID` smallint(5) unsigned NOT NULL auto_increment,
  `VOLY_ID` smallint(5) unsigned NOT NULL,
  `STUDENT` varchar(100) NOT NULL,
  `TEACHER` smallint(4) unsigned NOT NULL,
  PRIMARY KEY  (`ID`),
  KEY `Parent` (`VOLY_ID`),
  KEY `TEACHER` (`TEACHER`)

TABLE `teachers` (
  `ID` smallint(4) unsigned NOT NULL auto_increment,
  `TLNAME` varchar(50) NOT NULL,
  `TFNAME` varchar(50) NOT NULL,
  `TROOM` varchar(6) NOT NULL,
  PRIMARY KEY  (`ID`)

The volunteers hasMany students

The teachers hasMany students

The students  belongsTo volunteers
The students  belongsTo teachers


In my VolunteersController.php
I have an action for URL "cake/Volunteers/Show/2":

public function show($id = null)
{
   // Show Parents (Volunteers) and the associated students
if (!$id) 
{
throw new NotFoundException(__('Invalid volunteer'));
}
$volunteerData = $this->Volunteer->findById($id);
if (!$volunteerData) 
{
throw new NotFoundException(__('Invalid volunteer'));
}
$this->set('volunteer', $volunteerData['Volunteer']);
$this->set('students', $volunteerData['Students']);
}

How can I retrieve and display 'Teachers.FNAME' and 'Teachers.LNAME'  in place of the 'students.TEACHER' ( 'teacher.ID' ) from VolunteersController.php ?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: