Auto_Modeler_ORM Documentation¶
The ORM extension adds the following methods and properties to Auto_Modeler:
- $has_many and $belongs_to member variables to define related tables for many to many relationships
- overloaded __get() method for one to many relationships (your column needs "externaltable_id" convention [user_id])
- overloaded __set() method for one to many and many to many relationships
- find_related($key) method for retrieving many to many relationships
- remove($key, $id) to delete many to many relationships
- overloaded delete() method to handle many to many relationships
How to set up your database and model¶
Follow these steps to set your code up:
1. Your model needs to extend Auto_Modeler_ORM:
class Blog extends Auto_Modeler_ORM
2. Your model needs a $related_table variable with an array of your related tables:
protected $has_many = array('cars', 'boats'); protected $belongs_to = array('trains');
These tables all need an "id" field in them as your primary key.
3. Now you need to set up join tables in your database for your related tables. The format is "maintables_subtables". So for our blog example, we would create three tables: blogs_cars, blogs_boats and trains_blogs. In this table, you need three columns, id, maintable_id, subtable_id. So for our example, our columns would be: id, blog_id, car_id for the blogs_cars table; id, blog_id, boat_id for your blogs_boats table; id, train_id, blog_id for your trains_blogs table.
This is all the setup!
Methods¶
__set()¶
This is the standard assignment operator. You can use it to add related items to your model. You assign the id of the related item to the model. Usage:$blogs = new Blog_Model(5); // Get the blog post with ID=5 $blogs->user_id = 3; // Assign a new owner, for the next example $blogs->cars = 10; // Relates car with ID=10 to the current blog item with ID=5
__get()¶
This is the standard "fetch" operator. You can use it to fetch one to many relationships. If any of your database columns have an "_id" suffix, they are related to the table that comes before the suffix. This operator will fetch the related row with the id you specify. Usage:$blogs = new Blog_Model(5); echo Kohana::debug($blogs->user); // Will echo out the user details that are assigned to this blog entry (user_id = 3).
find_related($key, $where)¶
This method finds all $key objects related to the current model. Usage:$blogs = new Blog_Model(5); echo Kohana::debug($blogs->find_related('cars'); // Find all cars related to this blog entryYou can also specify an optional $where array to refine your related search:
$blogs = new Blog_Model(5); echo Kohana::debug($blogs->find_related('cars', array('make' => 'Honda')); // Find all honda cars related to this blog entry
find_parent($key)¶
This method finds all $key parent objects related to the current model. Usage:$blogs = new Blog_Model(5); echo Kohana::debug($blogs->find_parent('trains'); // Find all trains that are a parent to this blog entry
has($key, $value)¶
This method determines if the current model has a $key child with id = $value. Usage:$blogs = new Blog_Model(5); echo Kohana::debug($blogs->has('cars', 10); // Find if a car with id = 10 is related to this object (true)
remove($key, $id)¶
This method removes the relationship with id of $id from the $key table. Usage:$blogs = new Blog_Model(5); $blogs->remove('cars', 10); // Remove the relationship of blog ID=5 and car ID=10
remove_all($key)¶
This method removes all relationships with $key. Usage:$blogs = new Blog_Model(5); $blogs->remove_all('cars'); // Removes all the relationships with all cars
remove_parent($key)¶
Same as remove_all except this works on children. Usage:$cars = new Car_Model(10); $cars->remove_parent('blogs'); // Removes all the relationships with all parent blogs