Thursday, March 4, 2010

hasOne->HABTM save problem

I have the following relationships set up for a database of Samples.
There are many different types of sample and each type has it's own
set of fields so I have modelled them as Supertype/Subtypes with
hasOne relationships. For an ExportSample it can have one or more
documents tied to it.

Sample hasOne ExportSample
ExportSample HABTM ExportDocuments

Sample Model:
var $hasOne=array
(
'ExportSample';
)


ExportSample Model:
var $hasAndBelongsToMany=array
(
'ExportDocuments'
);

ExportDocuments Model:
var $hasAndBelongsToMany=array
(
'ExportSample '
);

I have a form that consists of some fields from the Sample, some from
the ExportSample and a list of checkboxes for the ExportDocuments .

echo $form->create('Sample',array('action'=>'addedit'));
echo $form->input('Sample.id');
echo $form->input('Sample.sampletype_id');
echo $form->input('ExportSample.samplesize_id');
echo $form->input('ExportSample.sampleform_id');
echo $form->input('ExportSample.id');
echo $form->input('ExportSample.ExportDocument', array( 'type' =>
'select', 'multiple' => 'checkbox', 'options'=>$lists['docs']));
echo $form->end('Save Changes');

In the controller I am saving the data with...

$this->Sample->saveAll($this->data);

When I use this form it saves the Sample data and the Export Sample
data from the form but it won't save the HABTM Export Documents. It
doesn't run any queries on the HABTM join table at all. If I leave out
the ExportSample.id it does save the HABTM data but with an
exportsample_id of 0. Put it back in and the HABTM data doesn't save.

I believe the relationships are set up correctly because running a
find operation on the Sample model will show the HABTM ExportDocument
data.

I have successfully used forms in the past with HABTM data and if the
Sample model had a HABTM relationship with Export Documents I would
have no problems - but in this case the hasOne->hasOne->HABTM
relationship seems to screw things up.

Has anyone come across this before or got any ideas about how I can
get this to work?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: