Need help understanding DataMapper and Relationships |
[eluser]xzela[/eluser]
Hey All, I'm a bit confused on how DataMapper handles existing data relationships. In other words, if you have a Map table that maps the relationships between two other data objects, how to do you describe that relationship to DataMapper? I've been searching the forums, stackoverflow, and google for quite a while now but have been unable to get a solid answer or example. So, here I am... Below is a simplified database schema and model descriptions: The Performer table describes any given performer, in this case, just their name Code: -- Performer table The Show table describes any given show, in this case, just a show title Code: -- Show table The PerformerShowMap table describes which performers are performing in which shows. Code: -- PerformerShowMap table The Performer Model Code: class Performer extends DataMapper { The Show Model Code: class Show extends DataMapper { As you can see from the above example, Show 1 has three performers applied to it (based on the information supplied in PerformerShowMap). When I access Show 1, I should be able to also get the three performers as well. So, my ultimate question is: How do I describe relationships between Show and Performer while using the PerformerShowMap table to DataMapper? Code: // in some random controller Any help on this subject would be great. Thanks
[eluser]WanWizard[/eluser]
Check out the docs (http://datamapper.wanwizard.eu/pages/relationtypes.html) on relationships. Unfortunately you didn't do this before you started working (you might want to check the rest of the manual too), because now you've chosen primary keys and foreign keys that are not compatible with Datamapper (it requires "id" as PK for every table, and FK's like "show_id" and "performer_id", not "ShowId" and "PerformerId"). If you standardize your design, then it's simply defining a has_many in both models, pointing to the other model, and you're off.
[eluser]xzela[/eluser]
Hey WanWizard, Thanks for replying. Unfortunately the database that i'm using was created a long time ago and many (many) tables do not follow the standard ORM setup. Very few tables actually have a primary key labeled id. In fact, the Performers table actually has emp_id as the primary key (because every Performer is actually an Employee, not sure who decided to call the table Performers!?). Other tools happen to use this same database, thus renaming columns would cause a lot of problems. So, perhaps I should be asking a different question: Is there any way to get DataMapper to handle these improper column names and structures? Such as, telling DataMapper to use emp_id as the primary key instead of the standard id. (and just to emphasize the database weirdness a bit more, the Show table primary key is shootid, note that lack of underscore and totally different word in this example. I guess that's what happens when you have 3 to 4 people independently working on a codebase over the course of 7 years.) Let me know what you think. Thanks.
[eluser]WanWizard[/eluser]
Short answer: no, there isn't. The 'id' bit (and the "_id" suffix on FK's) is hardcoded.
[eluser]Unknown[/eluser]
hi I want to ask about how DataMapper relate the models.. I have model like this: Code: class Model_Pelanggan extends DataMapper Table Pelanggan: id nama telepon created updated and Code: class Model_Transaksi extends DataMapper Table Transaksi: id pelanggan_id total created updated I alr tried code like in here too, but I don't know why it search for joining table as described in error message: Code: A Database Error Occurred
[eluser]yoelrodguez[/eluser]
Hello to correct the error models provides as follows: class Model_Pelanggan extends DataMapper { public $table = 'pelanggan'; var $has_many = array ( 'barang' => array ( 'class' => 'model_barang', 'other_field' => 'pelanggan', 'join_table' => 'barang_pelanggan' ), 'transaksi' => array ( 'class' => 'model_transaksi', 'other_field' => 'pelanggan_id' ) ); } class Model_Transaksi extends DataMapper { public $table = 'transaksi'; var $has_one = array ( 'pelanggan' => array ( 'class' => 'model_pelanggan', 'other_field' => 'transaksi' ) ); var $has_many = array ( 'transaksi_detail' => array ( 'class' => 'model_transaksi_detail', 'other_field' => 'id' ) ); } If you want to leave it as you just have to create the table that asks you which is the ratio of which corresponds to the ratio of one-to-many you have in the model |
Welcome Guest, Not a member yet? Register Sign In |