I have two tables:
contractors
=========
id
first_name
last_name
services
======
id
service_name
Then a table to join them together in a many-to-many relationship:
contractors_services
===============
id
contractor_id
service_id
In other words, a contractor can be associated with many services, and
a service can be associated with multiple contractors.
All I want is a list of all the contractors for a particular service.
In regular SQL, the query would look like this:
SELECT c.* FROM contractors c
JOIN contractors_services cs ON cs.contractor_id = c.id
WHERE cs.service_id = 2;
/* Where 2 is the service_id you're looking for */
So, how do I get this to work using the Cake method?
I have tried:
$data = $this->Contractor->findAll('contractors_services.service_id =
'.$service_id);
But it doesn't like that. Look at the query it wrote, it didn't
actually join the services table at all.
Here is my code:
class Contractor extends AppModel
{
var $name = 'Contractor';
var $hasAndBelongsToMany = 'Service';
}
class Service extends AppModel
{
var $name = 'Service'; // required for php4
var $hasAndBelongsToMany = 'Contractor';
}
class ContractorsController extends AppController
{
var $name = 'Contractors'; // for php4
var $helpers = array('Html', 'Javascript');
function index($service_id='')
{
// What do I need to put here to return all the
contractors linked to $service_id?
$data = $this->Contractor->findAll(); //'service_id =
'.$service_id);
$this->set('data', $data);
$this->render();
}
}
Can anyone see what I'm doing wrong or what more I need to do to be
able to get this simple query to work?
Thank you,
Brandon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---