I think i have found a bug tho i’m not sure.
I’m using
Codeigniter 1.7
Datamapper 1.5.4
I have created a table called products and to avoid conflicts trying to load a datamapper model (product) and a web page controller(product) [conflict = they have the same name] i have renamed my datamapper model to Db_product. I also have a table called agents and the two are related. An agent can have many products and a product can have many agents.
Thusly, I have a table called rel_agents_products (rel_ is my join prefix).
Next i have added the table variable in the db_product obviously so it knows which table to reference.
Now the problem…
I used the get() method on db_agent to call my agent data. then i attempted to load all related products for that agent.
$agent = new Db_agent();
$agent->get_by_id(1); // << My agent ID.
$agent->db_product->get(); << get the related products using my db_product dm model
when stepping through the code I found that the relationship was trying to use the db_product as the field name in the relationship ie. products.db_product_id.
obviously this isnt correct as the relationship table consists of id, agent_id and product_id.
I was forced to modify the Datmapper library to check if the table var has been set and so use a singular version of the table var, else use the model name.
Is this correct or have I solved a problem that there is another way around?
This is what i have changed in the Datamapper library.
Line 2104
was..
$this->db->join($relationship_table, $this->table . '.id = ' . $relationship_table . '.' . $this->model . '_id', 'left');
..which i changed to..
$this->db->join($relationship_table, $this->table . '.id = ' . $relationship_table . '.' .(isset($this->table)?singular($this->table):$object->model) . '_id', 'left');
and
line 2105
was..
$this->db->join($object->table, $object->table . '.id = ' . $relationship_table . '.' . $object->model . '_id', 'left');
..which i changed to..
$this->db->join($object->table, $object->table . '.id = ' . $relationship_table . '.' . (isset($object->table)?singular($object->table):$object->model) . '_id', 'left');
I hope i just made sense!?
Sorry about the long post.
😊