I have a database that uses the Party analysis pattern to simplify associations (as it has to store info on both people and organisations, both of which have common attributes). Therefore my associated models can get quite deep. My question is what is the best way to save my data? Take for example saving an employee, the Employee table is not directly connected to Party table, however if I nest my models in an array like so:
array(
'Party' => array(
'Email' => array(...),
'Address' => array(
'Country' => array(...),
'Location' => array(
'State' => array(...)
)
),
'PhoneNumber' => array(...),
'Person' => array(
'Language' => array(...),
'Skill' => array(...),
'Employee' => array(
'WorkHistory' => array(...),
'Qualification' => array(...)
)
)
)
)
I can use saveAll, and it will save everything. That means on my forms I have field names such as Party.Person.Employee.start_time for example. However I was told that doing it this way is not really following CakePHP conventions. The other alternative is to have my array like this:
array(
'Party' => array(...)
'Email' => array(...),
'Address' => array(...),
'Country' => array(...),
'Location' => array(...),
'PhoneNumber' => array(...),
'Person' => array(...),
'Language' => array(...),
'Skill' => array(...),
'Employee' => array(...),
'WorkHistory' => array(...),
'Qualification' => array(...)
)
However if I call saveAll on this, then not everything will be saved as some of the tables/models are not directly connected to Party. The only way to save such a structure, that I know of is, to use Transactions.
So I was wondering what the CakePHP experts think is the best way to approach a situation where you need to save very deep associated models?
Edit:
From what I can gather based on the responses I have had, for the model you are calling saveAll on, those other models that are directly associated to it do not need to be nested in that models array. However if you also want to save models that are not directly associated to it then you need to nest those inside a model that is directly associated to the model you are calling saveAll on. Sorry I know that's confusing so here is my revised array structure in light of this information:
array(
'Party' => array(...),
'Email' => array(...),
'Address' => array(
'Country' => 'attribute',
'Location' => 'attribute',
'State' => 'attribute'
),
'PhoneNumber' => array(...),
'Person' => array(
'Language' => array(...),
'Skill' => array(...),
'Employee' => array(
'WorkHistory' => array(...),
'Qualification' => array(...)
)
)
)
Person is directly associated to Party and so does not need to be nested within Party. However Employee (a type of Person) is not directly associated to Party, and so for it to save the Employee data when calling saveAll on Party, it needs to be nested within Person because the Person model is directly associated to Party. (FYI: Party hasOne Person and Person hasOne Employee. This is the best way I could figure out to structure the inheritance in my database). By structuring it this way, the application is both conforming to CakePHP standards and saving the data with minimal effort.