CodeIgniter Forums
DataMapper And Tables Separated By Underscores? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper And Tables Separated By Underscores? (/showthread.php?tid=44317)



DataMapper And Tables Separated By Underscores? - El Forum - 08-11-2011

[eluser]Vheissu[/eluser]
Obviously this is a WanWizard question but if anyone else knows the answer your help would be gratefully appreciated.

Basically I have a table called 'products' and then a table called 'product_categories' a product can have many categories, so usually with DataMapper you would create a join table right? So following convention the join table would be called "products_product_categories", would this work or is there something else I need to do?

In the join table, do my field names need to be named differently and do I need to add any parameters to my models?


DataMapper And Tables Separated By Underscores? - El Forum - 08-11-2011

[eluser]WanWizard[/eluser]
Datamapper doesn't care if there's an underscore in the table name or not.

It just follows the rules set in the userguide: glue the two table names together with an underscore, alphabetical lowest first (that might be product_categories, is an underscore lower than an 's'?). For the field names, the same is true. tablename + '_id' is the foreign key.


DataMapper And Tables Separated By Underscores? - El Forum - 08-11-2011

[eluser]Vheissu[/eluser]
Ah, thank you. That makes perfect sense. Question about the foreign key though, you mention it's the table name underscore ID. So if I have a 2 tables one called "products" and another called "product_categories" and have a join table called "products_product_categories". What foreign key name would DataMapper expect for "product_categories"? Would it expect "product_categorie" as the foreign key in the join table or could I use "product_category"?

Are there times like above where you need to tell the model what the foreign key is for each table so when you create join tables it knows? Sorry about all of the questions, just trying to learn the best way to do things with DataMapper, it's excellent.


DataMapper And Tables Separated By Underscores? - El Forum - 08-12-2011

[eluser]WanWizard[/eluser]
You'll have to test that, it depends on the inflector used. My guess would be 'product_category_id'.

You can override it when defining an advanced relationship, as described in the manual.