![]() |
Datamapper: more direct access to join fields - 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: more direct access to join fields (/showthread.php?tid=50234) |
Datamapper: more direct access to join fields - El Forum - 03-19-2012 [eluser]Dracos[/eluser] I've got two models, let's call them A and B, in a one to many relationship (A has one B, B has many A's). The A_B table has extra fields, including A_B.foo. I know that I can do this: Code: $a = new A($id); The result is all the fields of B plus the join fields, because the query begins Code: SELECT B.*, A_B.id as join_id, A_B.foo as join_foo [...] My question: is there a way, having instantiated A, to get the A fields with the join fields, and not reference B at all? Such as Code: $a = new A($id); The reason I ask is that I will be dealing with sizable sets of $a->get() results where I want to include the A_B join fields, but I don't actually want the B fields, or to even instantiate B. Datamapper: more direct access to join fields - El Forum - 03-20-2012 [eluser]WanWizard[/eluser] In more complex cases with join fields I usually split the relation. So instead of having a many-many between A en B, I create a many to one between A and AB, and a many to one between B and AB (where AB is your relationship table). Create a model for your relationship table, and you can query it like any normal relation. Downside of this is that you have to either use deep relations (AB->B) when querying B from A, or define two relations, one from A to B (many to many) and one from A to AB (many to one). Datamapper: more direct access to join fields - El Forum - 03-20-2012 [eluser]Dracos[/eluser] Thanks WanWizard. It would seem to me that modifying include_join_fields() to take $model as an argument (class name string, instance, or an array of these) could solve this (also optionally a second argument for a list of desired join fields). Or introducing a new function the achieve the same end. Code: $a = new A($id); Datamapper: more direct access to join fields - El Forum - 03-20-2012 [eluser]WanWizard[/eluser] It's not really natural to use custom methods for this functionality. The next version of DM will not have them anymore, but will use the model approach (where you will define a A many-to-many B through AB). |