views:

142

answers:

4

I'm using CakePHP to build an application using the MVC Pattern, but my question is language agnostic, I guess.

Here's an overview of a part of my data:

ITEMS (id, name, description)
LOCATIONS (id, name)

The items table describes a type of item ("apple", "orange", "banana"), not individual items.

Each location can have any number of each item. To hold this many-to-many relationship, I have this table:

ITEMS_LOCATIONS (id, item_id, location_id, quantity)

// eg (1, "home", "apple", 5) --> I have 5 apples at home
//    (2, "work", "apple", 2) --> I have 2 apples at work
//    (1, "home", "orange", 1) --> 1 orange at home
// (Don't worry - I have numeric ids... this is just an example)

Now I need to be able to edit this "inventory" information, but I'm not sure exactly which model or controller should be handling it?

Should the locations controller have an editInventory action? Or should the items controller have editLocations or what? Should I make a brand new model?

+1  A: 

This is kind of subjective really.

You definitely want to have an Edit action on your Inventory controller for editing Inventory item details and an Edit action on your Location controller for editing Location details.

When it comes to managing the relationship you probably want to think about how you work with that relationship - are you generally likely to be adding Locations to an Item, in which case you may want an AddLocation action on your Items controller, or are you more likely to be adding items to a Location in which case you probably want an AddItem action on your Location controller. Maybe you go both ways and in this case maybe it is feasible to create an ItemLocations controller and do the work there....

HTH

Simon Fox
+1  A: 

There are no real hard and fast rules for this. Use what makes most sense to you.

In your case, you probably want to control the Items at a Location, so having a URL like /location/42/items or /location/42/inventory or /location/42/items/edit would probably make the most sense. Ergo, make an appropriate action in your LocationsController.

The other way around, if you want to control at which location an item is, /item/42/locations probably makes more sense.

If both are equally "important", you may want to make an extra controller for it and use /inventory/edit/42, whatever that specifically means to you.

As for the model, you can prep your $data either way so that it updates the HABTM model when using saveAll(), or you can manipulate the HABTM model directly like $this->Item->ItemsLocation->save(). Usually you don't need to bake the HABTM model, unless you need it connected to the other models.

deceze
A: 

I would tend to pass a location id to an the items controller, and have it store this as a foreign key in the item record.

John Lockwood
it's a many-to-many (hasAndBelongsToMany) relationship though. Each location can have many items, and each item can have many locations, so I can't just store locationId in the items table
nickf
+2  A: 

There is no convention as such, for which model or controller to use. You can always use the "uses" array to include models. Once the models are included you can do whatever you want from any of the controller. In fact if you want you can only use a single controller for all your job.

Hope this works.

Hasan