I've been working with CakePHP (2.x) for quite a while and I'm loving it. 
However, I found some very important aspects covered too briefly in the 
documentation. When googling around and asking in other forums, I got mixed 
responses, increasing my feeling of uncertainty even more.

I'd like to ask this group to help me (and possibly others) clarifying a 
few questions. I've used Pastie for code as I frequently find long code 
snippets in group posts hurting readability.

Here we go:

Assume I've got two models, User and Group related to each others in a 
HABTM relationship. Each user can have multiple groups and groups belong to 
multiple users. My simple model definition <http://pastie.org/4307801> is 
here: http://pastie.org/4307801. Also, the join table *groups_users* exists 
with the appropriate columns following CakePHP's conventions.

1. Question:
a) Assuming that all tables are empty, can I add a *new user* along with *a 
single new group* and the corresponding entries in the join table in one 
*single 
command*? I tried this http://pastie.org/4307843. The user gets created, 
but no group nor does anything get inserted into the join table.

b) In case I did anything wrong in a) and it is in fact possible to create 
a User and Groups on the fly in one single command, is it also possible to 
create *multiple* groups? If so, would this http://pastie.org/4307863 be 
the correct way of doing it?

c) Is this what *saveAll()* and *saveAssociated()* where meant for?

2. Question:
What exactly is the difference between *save()*, *saveAll()* and *
saveAssociated()*?

3. Question:
In past versions of CakePHP it was difficult to have CakePHP add *additional
* related items (= records to the join table) as CakePHP annoyingly deleted 
any existing records in the join table before adding new ones for an 
existing relationship. If, e.g. the user would join additional groups, 
there was no easy way doing it in CakePHP besides taking care for inserting 
the correct records in the join table yourself. As far as I remember 
somebody wrote a behavior to circumvent this but it didn't get updated and 
no longer works.

What is the best way in CakePHP to add additional related objects (not new 
relationships!) when some already exist? Am I supposed to just manually *
save()* records to the join table using the implicit join table model?

4. Question:
Last but not least: Give the above and the use case, how would a 
professional, experience CakePHP developer handle the following use cases 
(code welcomed!):

a) Adding the first group to an existing user.
b) Later, adding additional groups to that user.
c) Later, removing some of the groups.

I hope to hear back from the community. You can also find me on Twitter 
@ralf <http://twitter.com/ralf>.



-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to