Tuesday, February 23, 2010

Re: Saving related table data

I am in the process of acquainting myself with Cake and have come
across a question related to `koala kid`'s. Namely, to explore the
$hasOne relationship:

-- I baked two models, Parental and Dependent, where parental_id is a
foreign key in `dependents` that references `parentals`.`id`.
-- Apart from `id`s, each model only has a `name` field.
--When I set the Parental model to $hasOne Dependent, I able to use
the standard baked controllers to add more than one Dependent to a
Parental. The Dependent model is associated by $belongsTo. Why is this
the standard behavior for this association?

On Feb 12, 11:04 am, koala kid <alexhob...@gmail.com> wrote:
> Hi John,
>
> thanks for your fix, its now working. I had assumed that as I was
> defining the relationship as ahasOneand belongsTo Cake would
> automatically know to Update rather than Insert as it was a 1 - 1
> relationship, using the Rental.id.
>
> This is where I am really struggling with Cake, so much is taken care
> for you that you don't know where it stops, and the documentation,
> although much improved, still makes it hard on occasion to find out.
>
> Anyway thanks again.
>
> On Feb 11, 3:02 am, John Andersen <j.andersen...@gmail.com> wrote:
>
>
>
> > Your problem lies in this part:
>
> >     [Rate] => Array
> >         (
> >             [s1_title] => title 1
> >             [s1_day] => day 1
> >             [s1_week] =>
> >             [s1_month] =>
> >             [extra_info] =>
> >         )
>
> > I assume that this information was at first retrieved from the
> > database and presented to the user. The user then changed something
> > and saved.
>
> > The above information lacks the Rate.id information - without which
> > CakePHP assumes that you are saving a new record in the Rate model.
>
> > You have to add the Rate.id as a hidden field in the view (worst
> > solution) or get it from somewhere else.
> > Hope this will help you on the way,
> >    John
>
> > On Feb 10, 8:09 pm, koala kid <alexhob...@gmail.com> wrote:
>
> > > Hi, I'm still struggling with saving information to related tables. I
> > > have a form which updates info about a rental property and I have an
> > > associated Rates table which holds data about the rental rates for the
> > > property.
>
> > > I've defined my relationship as :
>
> > > Rental ->hasOne
> > > Rate -> belongsTo
>
> > > My save action looks like this:
>
> > > $this->Rental->saveAll($this->data);
>
> > > However this save action is creating a new entry in the Rates table
> > > rather than updating the existing one. I'm pretty sure my form data is
> > > in the correct format:
>
> > > Array
> > > (
> > >     [Rental] => Array
> > >         (
> > >             [id] => 45
> > >             [rental_code] => PDSSerena
> > >             [location_id] => 19
> > >             [rental_agent] =>
> > >             [rental_meta_url] =>
> > >             [rental_meta_title] =>
> > >             [rental_meta_desc] =>
> > >             [rental_meta_tags] =>
> > >         )
>
> > >     [Rate] => Array
> > >         (
> > >             [s1_title] => title 1
> > >             [s1_day] => day 1
> > >             [s1_week] =>
> > >             [s1_month] =>
> > >             [extra_info] =>
> > >         )
>
> > > )
>
> > > Do I need to explicitly set the rental_id in my Rate form data, such
> > > as:
>
> > > [Rate] => Array
> > >         (
> > >             [rental_id] => 45
> > >         )

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: