-
Notifications
You must be signed in to change notification settings - Fork 10
Auto_Modeler_ORM
The ORM extension adds the following methods and properties to AutoModeler:
-
$_has_manyand$_belongs_tomember variables to define related tables for many to many relationships - overloaded
__get()method for one to many relationships (your column needs "child_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
Follow these steps to set your code up:
- Your model needs to extend AutoModeler_ORM:
class Blog extends AutoModeler_ORM
- For many to many relationships, your model needs a $_has_many and _belongs_to variable with an array of your related tables:
protected $_has_many = array('cars', 'boats');
protected $_belongs_to = array('trains');
These models all need an "id" field in them as your primary key.
- Now you need to set up join tables in your database for your related tables. The format is "parents_childs". 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, parent_id, child_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!
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
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).
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 entry
You can also specify an optional $where object to refine your related search:
$blogs = new Blog_Model(5);
echo Kohana::debug($blogs->find_related('cars', db::select()->where('cars.make', '=', 'Subaru'))); // Find all subaru cars related to this blog entry
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
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)
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
This method removes all relationships with $key. Usage:
$blogs = new Blog_Model(5);
$blogs->remove_all('cars'); // Removes all the relationships with all cars
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
AutoModeler_ORM comes with robust support for load()ing relationships with() the main model. This works with one to many relationships (you need a model_id column in your table). Suppose you have a Model_User with a foobar relationship:
$user = new Model_User();
$user->with('foobar')->load(db::select()->where('users.id', '=', 1));Now when you call $user->foobar, it will not load an additional query. You must use with() alongside of load().
If you'd like a model to always load with another model, you can manually specify the $_load_with variable in the model:
protected $_load_with = 'foobar';