I have a simple crud application in Kohana 3.3, with a few different types of data or models. Let’s say those models are:
- Users
- Locations
- Skills
I have a table for each of those models, but the tables aren’t related to one another in any way. I’ve been trying to define relationships with ORM, but I’m still confused. As an example:
I have a number of locations. Each location has many users.
I know I can define that with:
class Model_Location extends ORM {
/**
* A location has many users
*
* @var array Relationships
*/
protected $_has_many = array(
'users' => array('model' => 'user'),
);
}
As I understand it, I can connect the two by referencing the ID of the parent location from the row in the user table. However, what if each user can belong to many locations? Am I supposed to store serialised data in a foreign key? Should I create a “look-up table”? If so, how should it look?
How can I for example, query the database for a location and all users attached to it? Is ORM even the right technology to be using for this kind of thing?