There are examples on the Grocery CRUD website of a one-to-one relationship and a many-to-many relationship, but no example of a one-to-many relationship.
Here is an example: I have car_makes
and car_models
tables. Each model_id
may only belong to one make_id
. Each make_id
can be associated with many model_id
s.
SQFiddle: http://sqlfiddle.com/#!9/c357c
Now, I try to set up Grocery CRUD:
public function cars(){
$crud = new grocery_CRUD();
$crud->set_table("car_makes");
$crud->set_relation("make_id","car_models","model_name");
$output = $crud->render();
$this->_example_output($output);
}
This doesn't work -- in the grid view, the make_id
is suddenly replaced with a model_name
, and it's totally wrong. For example, I'm seeing the "Ford Volt" when I clearly specified the model_name
Volt is associated with make_id
2, and make_id
2 has a make_name
of Chevrolet. When viewing a single record, only one make is visible, when there should be a list.
What I really want to do is make it easy to add additional models to car makes, and see what models are already associated with a make.
How should I configure Grocery CRUD to handle this 1-n relationship?
I need to avoid a many-to-many relationship, because a model may not belong to more than one make. Thanks.
EDIT: A colleague suggests handling models through a model list instead of part of the makes list would be the way to go. This seems reasonable, but less useful.
EDIT 2: Here's the code that allows you to look up a model and assign a make to it:
public function car_models(){
$crud = new grocery_CRUD();
$crud->set_table("car_models");
$crud->set_relation("make_id","car_makes","make_name");
$output = $crud->render();
$this->_example_output($output);
}
Now, I really think it should be the other way around -- you look up a make and assign models to it -- but I don't think Grocery CRUD supports that for 1-n relationships when using set_relation(). That is supported for many-to-many relationships, where you can look up 1 movie and assign many actors to it. Perhaps by using set_relation_n_n() this can be accomplished?