Datamapper : One To Many |
[eluser]Flyingbeaver[/eluser]
Hi ![]() I need help on this plz : 2 tables Service : has many Sub_Service Sub_Service : has one Service My code : Code: $services = new Service(); But it doesn't work, if I want to access sub_service i have to take it out of the first loop and do somthing like Code: foreach($services->sub_service as $sub_service) But this is not what I want, i want to get an array like this one : Service 1 --Subservice 1 --Subservice 3 Service 2 Service 3 --Subservice 2 Info i'm using DM 1.8 and CI2. Thx for ur help ![]()
[eluser]Flyingbeaver[/eluser]
On another topic i'v seen this : Code: function test() That's a solution but it doesn't seem very clean and very fast for many results.
[eluser]WanWizard[/eluser]
Datamapper is object based, so every query starts from the current object. What you want is not to get a single object, but all objects for a model, and for each of these objects, fetch all childs. This functionality is not in Datamapper because it would crash your application with an out-of-memory error very quickly, if it has to instantiate all those objects. You need to "flatten" the result, as you would do with a normal JOIN query. That requires the include_related() method, but currently that is limited to $has_one relations. However, I don't think it breaks on a $has_many relation. You could try to comment the error message in line 4656, and use Code: $c = new Chain();
[eluser]ramm[/eluser]
Same problem i have (actually that's my example). Like i said in the other post, i got it working but it doesn't seem clean. In the other hand, i'm try to use Smarty3 for the templates. In that example, what should go after the include_related?
[eluser]WanWizard[/eluser]
See the docs. It needs a model name, and optionally selection criteria. It produces a query like Code: SELECT a.*, b.* from a JOIN b WHERE a.id = b.a_id You can then loop over it in one go. As all 'a' values will be repeated, you'll have to make your own 'break' logic, like you would do with a normal AR query.
[eluser]Flyingbeaver[/eluser]
[quote author="WanWizard" date="1298235727"]Datamapper is object based, so every query starts from the current object. What you want is not to get a single object, but all objects for a model, and for each of these objects, fetch all childs. This functionality is not in Datamapper because it would crash your application with an out-of-memory error very quickly, if it has to instantiate all those objects. You need to "flatten" the result, as you would do with a normal JOIN query. That requires the include_related() method, but currently that is limited to $has_one relations. However, I don't think it breaks on a $has_many relation. You could try to comment the error message in line 4656, and use Code: $c = new Chain(); perfect ! thx !
[eluser]denis747[/eluser]
[quote author="WanWizard" date="1298235727"]Datamapper is object based, so every query starts from the current object. What you want is not to get a single object, but all objects for a model, and for each of these objects, fetch all childs. This functionality is not in Datamapper because it would crash your application with an out-of-memory error very quickly, if it has to instantiate all those objects. You need to "flatten" the result, as you would do with a normal JOIN query. That requires the include_related() method, but currently that is limited to $has_one relations. However, I don't think it breaks on a $has_many relation. You could try to comment the error message in line 4656, and use Code: $c = new Chain(); In which file should I comment the error message? is it on the datamapper file stored in the application/libraries? Thanks, Denis.
[eluser]denis747[/eluser]
Thanks is this correct? I have commented this on line 4618 on mine it shows up from 4618. I am using datamapper version 1.8 : // show_error("Invalid request to include_related: $rf is not a has_one //relationship to {$last->model}.");
[eluser]WanWizard[/eluser]
Looks like it. This is the change that lifts the ban: https://bitbucket.org/wanwizard/datamapp...b40ef4073f. You can also download 'tip' from bitbucket to test the latest version. |
Welcome Guest, Not a member yet? Register Sign In |