> On Dec 17, 2:54 pm, Rob Wilkerson <[email protected]> wrote:
>
> > I have what I suppose is a 3-level model relationship.  I have a
> > vendors table that is "extended" by a commercial_vendors table.  A
> > commercial_vendor "is-a" vendor so the commercial_vendors table's
> > primary key is vendor_id which is also a foreign key referencing the
> > vendors table's id field.  Additionally, all vendors of any "subtype"
> > have an address. Since other domain entities also have addresses, I've
> > normalized that data into its own table cleverly called "addresses".
> > The vendors table, then, has an address_id field that is a foreign key
> > referencing addresses.id.
>
> > The key table structure looks like this:
>
> > addresses
> >    id (PK)
> >    street_address_1
> >    ...
> >    zip_code
>
> > vendors
> >    id (PK)
> >    address_id (FK)
> >    business_name
> >    ...
> >    created
> >    updated
>
> > commercial_vendors
> >    vendor_id (PK,FK)
> >    type
> >    ...
>
> > The model associations, in turn, are defined as:
>
> > Address hasOne Vendor
> > Vendor hasOne CommercialVendor
> > CommercialVendor belongsTo Vendor
>
> > I tried creating a relationship where Vendor belongsTo Address, but
> > when the scaffolding couldn't handle it (it errored trying to find
> > addresses.vendor_id, as I recall), I figured I'd done something I
> > shouldn't.
>
> > Given all of that, I have my users creating a new commercial vendor
> > through /commercial_vendors/apply whose form action is /
> > commercial_vendors/add.  When, in my CommercialVendor model, I try to
> > $this->CommercialVendor->saveAll ( $this->data ), I get a foreign key
> > error. Seems Cake can't save my Vendor because the foreign key to
> > addresses fails.  Here's my form code in views/commercial_vendors/
> > apply.ctp:
>
> > <?php echo $form->create ( 'CommercialVendor' ) . "\n"; ?>
>
> > <fieldset class="first">
> >         <legend>Applicant Information</legend>
>
> >         <?php
> >                 echo $form->input ( 'Vendor.business_name' ) . "\n";
> >                 echo $form->input ( 'Address.street_address_1' ) . "\n";
> >                 echo $form->input ( 'Address.street_address_2' ) . "\n";
> >                 echo $form->input ( 'Address.city' ) . "\n";
> >                 echo $form->input ( 'Address.state' ) . "\n";
> >                 echo $form->input ( 'Address.zip_code' ) . "\n\n";
>
> >                 echo $form->input ( 'Vendor.applicant_title' ) . "\n";
> >                 echo $form->input ( 'Vendor.applicant_first_name' ) . "\n";
> >                 echo $form->input ( 'Vendor.applicant_last_name' ) . "\n";
> >                 echo $form->input ( 'Vendor.email' ) . "\n";
> >                 echo $form->input ( 'Vendor.phone_number' ) . "\n";
> >         ?>
> > </fieldset>
>
> > <?php
> >         echo $form->end (
> >                 array (
> >                         'label' => 'Apply',
> >                         'id' => 'commercial-vendor-save'
> >                 )
> >         );
> > ?>
>
> > Am I doing something wrong in my associations or am I asking more than
> > I should of saveAll()? I realize that there's no commercial vendor
> > info in the form. I'm trying to do this one step at a time so I can be
> > sure I understand the problems as they arise.

I've made a couple of changes to this based on an article at
http://teknoid.wordpress.com/2008/08/01/practical-use-of-saveall-part-1-working-with-multiple-models/.
Now my Vendor model belongsTo my Address model.  I've also updated my
form so that the fields are Vendor.0.field_name and Address.
0.field_name. Unfortunately, it's still not working.

Warning (512): SQL Error: 1452: Cannot add or update a child row: a
foreign key constraint fails (`dbname/vendors`, CONSTRAINT
`vendors_ibfk_1` FOREIGN KEY (`address_id`) REFERENCES `addresses`
(`id`) ON DELETE CASCADE ON UPDATE CASCADE) [CORE/cake/libs/model/
datasources/dbo_source.php, line 521]

Shouldn't saveAll() be inserting the Address record?  It's ID is a char
(36) (guid), so I'm assuming that Cake will handle that and save the
associated Vendor record using the correct ID.  If anyone can see what
I'm missing, I'd certainly appreciate the insight.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to