> Is it correct to associate my models such that:
>
> commercial_vendor belongsTo vendor
> organizational_vendor belongsTo vendor
Yes, in the first application I am working on with CakePHP I have the
following tables
organisations
people
agencies
households
schemes
agency_contacts
clients
staff
I have created the belongsTo relationships as follow:
Agency, Household and Scheme belongsTo => 'Organisation'
AgencyContact, Client, Staff belongsTo => 'Person'
And the reverse hasOne relations:
Organisation hasOne => array('Agency', 'Household', 'Scheme')
Person hasOne => array('AgencyContact', 'Client', 'Staff')
As one organisation or person can only relate to any one of the
allowed models (organisation cannot be a scheme and an agency) it
would possibly have been better to have the organisation or person
belong to the related model and include 'parent_model' and 'parent_id'
fields in the organisations/people tables. This way I could have
created the relationships as follows:
class Organisation extends AppModel {
var $name = 'Organisation';
var $belongsTo = array(
'Agency' => array(
'className' => 'Agency',
'foreignKey' => 'parent_id',
'conditions' => array('Organisation.parent_model' => 'Agency')
),
'Household' => array(
'className' => 'Household',
'foreignKey' => 'parent_id',
'conditions' => array('Organisation.parent_model' => 'Household')
)
),
'Scheme' => array(
'className' => 'Scheme',
'foreignKey' => 'parent_id',
'conditions' => array('Organisation.parent_model' => 'Scheme')
)
);
}
class Person extends AppModel {
var $name = 'Person';
var $belongsTo = array(
'AgencyContact' => array(
'className' => 'AgencyContact',
'foreignKey' => 'parent_id',
'conditions' => array('Person.parent_model' => 'AgencyContact')
),
'Client' => array(
'className' => 'Client',
'foreignKey' => 'parent_id',
'conditions' => array('Person.parent_model' => 'AgencyContact')
)
),
'Staff' => array(
'className' => 'Staff',
'foreignKey' => 'parent_id',
'conditions' => array('Person.parent_model' => 'Staff')
)
);
}
class Agency extends AppModel {
var $name = 'Agency';
var $hasOne = array(
'Organisation' => array(
'className' => 'Organisation',
'foreignKey' => 'parent_id',
'conditions' => array('Organisation.parent_model' => 'Agency')
);
}
class AgencyContact extends AppModel {
var $name = 'AgencyContact';
var $hasOne = array(
'Person' => array(
'className' => 'Person',
'foreignKey' => 'parent_id',
'conditions' => array('Person.parent_model' => 'AgencyContact')
);
}
Hope this makes some sort of sense ;)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" 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?hl=en
-~----------~----~----~----~------~----~------~--~---