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 (one in Parental, two in Dependent), each model
has a `name` field. There are no other fields.
Here's the part I find strange:
-- When I set the Parental model to $hasOne = 'Dependent', I am still
able to use the standard baked controllers to add more than one
Dependent to a Parental.
To be specific, assuming I've created a Parental with 'id'='1',
then I can use http://example/app/dependents/add repeatedly to add
many dependents with 'id' = 1. With the pre-baked controller and view,
I am asked to enter the parental_id manually in an input box, and
every time I set the id to 1 it makes a new dependent record,
_despite_ the $has*One* association. Which is strange to me, because I
supply the parental_id explicitly, and it adds it nonetheless. I
understand that the pre-baked controller uses Model->create() for the
add action, but still... In a $hasOne situation, shouldn't create() or
the subsequent save() logically throw some sort of error or boolean
false if there already exists a dependent with the same parental_id?
That said, the $hasOne behavior is even more unintuitive (from this
newbie perspective) when the situation in the underlying model tables
is more accurately reflect by $hasMany, for example as a result of the
process above. Assuming you set the association to $hasOne, but
actually "haveMany" in the associated table, you get results like
this:
[1] => Array
(
[Parental] => Array
(
[id] => 2
[name] => foster_parent
)
[Dependent] => Array
(
[id] => 8
[parental_id] => 2
[name] => test.a
)
)
[2] => Array
(
[Parental] => Array
(
[id] => 2
[name] => foster_parent
)
[Dependent] => Array
(
[id] => 9
[parental_id] => 2
[name] => test.b
)
)
This seems to be the result of some combinatoric/iterative process
that binds the parent to it's multiple dependents one at a time, each
time in a new array, and produces all possible combinations. Setting
the association to $hasMany, of course, returns the proper, as-
documented form of the $hasMany model output you'd expect:
[1] => Array
(
[Parental] => Array
(
[id] => 2
[name] => foster_parent
)
[Dependent] => Array
(
[0] => Array
(
[id] => 8
[parental_id] => 2
[name] => test.a
)
[1] => Array
(
[id] => 9
[parental_id] => 2
[name] => test.b
)
)
)
It's so strange to me that (a) the model lets you even add multiple
dependents to one parent in $hasOne mode, and then (b) then
combinatorially returns all possible associations with its all of its
dependents as shown above. I understand this allows the controller
which expects one result to get that one result (some result, who
knows which), and if programmed a certain way, not fault in the
process, but that just seems a bit odd to me.
I am ever mindful of my newness to Cake, and any insights are
appreciated!
Thanks,
Dmitry
P.S. I may have submitted an early version of this by clicking "Send"
accidentally. Apologies if I did.
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:
Post a Comment