views:

103

answers:

1

Hi! If i want to add a record to TABLE A, is there an efficient way to add a record in the JOIN TABLE for many (or all) records in TABLE B?

I try to build a simple task management (in CakePHP). An user adds a task and there will be added a connection to each other user in the same group as the current user.

At the moment, I use the find('list')-method to retrieve the IDs and store them in a variable. But I think if the groups grow, the PHP cache won't handle this amount of data in a single variable.

A: 

You should look into the Cake Has-And-Belongs-To-Many (HABTM) relationships. Check the cake book. It will allow you to create a relationship between two tables, using a join table, and will automatically retrieve and save values as requested in your application.

Please note, however, that from one model you cannot (by default) filter by criteria on the related model (in this case, the model related by the join table). To do this, you will need to use the Containable behavior, which will allow you to set filter criteria on the related tables.

The one other caveat is that I don't know of a way (out of the box) to add some information about the relationship in the join table. For example, if you wanted to record (in the join table) whether the user has completed the task, you would have to write your own stuff there. I usually get around this by creating a model for the join table, which I then invoke whenever I want to retrieve data specific to the joined relationship. By doing it this way, you can also easily pull up the data from the joined tables. Anybody else have a better solution?

Travis Leleu
I am already familiar with the principles of HABTM relations and how to deal with them in cakephp.I asked for a way to process a large number of new relations without running out of cache.
Benedikt R.