Welcome Guest, Not a member yet? Register   Sign In
Datamapper : One To Many
#1

[eluser]Flyingbeaver[/eluser]
Hi Wink

I need help on this plz :


2 tables

Service : has many Sub_Service
Sub_Service : has one Service

My code :
Code:
$services = new Service();
        $services->where('org_id', $org_id)->get();
        $services->sub_service->get();

        foreach($services as $service)
        {
            echo $service->title;

            foreach($service->sub_service as $sub_service)
            {
                      echo $sub_service->title;
            }
        }

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)
            {
                   echo $sub_service->title;
            }

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 Wink
#2

[eluser]Flyingbeaver[/eluser]
On another topic i'v seen this :

Code:
function test()
    {
        $c = new Chain();
        $c->get();
        
        foreach($c as $chain)
        {
            echo "<h3>".$chain->nme."</h3>";
            $r = new Restaurant();
            $r->where('chain_id', $chain->id)->get();
            foreach($r as $rest)
            {
                echo $rest->name."<br />";
            }
        }
    }

That's a solution but it doesn't seem very clean and very fast for many results.
#3

[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();
$c->include_related('restaurant')->get();
check_last_query();
#4

[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?
#5

[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.
#6

[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();
$c->include_related('restaurant')->get();
check_last_query();
[/quote]

perfect ! thx !
#7

[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();
$c->include_related('restaurant')->get();
check_last_query();
[/quote]

In which file should I comment the error message? is it on the datamapper file stored in the application/libraries? Thanks,
Denis.
#8

[eluser]WanWizard[/eluser]
In the library datamapper.php, line 4656.
#9

[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}.");
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB