views:

29

answers:

1

For a website i'm building I need to check wether a location has contracts, these locations can have contracts linked directly to them or when there part of a larger organisation also have contracts that are linked to the organisation.

I am trying to use a DQL query to first check if there are contracts linked directly and then if there are any linked via an organistion with this query:

    $q = self::createQuery("l")
        ->select('sl.sc_id, sl.type, so.sc_id, so.type, l.naam, l.loc_id, l.straat, l.telefoon, l.plaats, l.postcode, l.huisnummer, l.huisnummer_achtervoegsel, o.naam')
        ->from('Locatie l, l.Organisatie o, l.Sc sl, o.Sc so')
        ->orderBy('o.naam, l.naam')
        ->execute();

It does what I want in so much as that it returns an array with all the data I need:

array(13) { 
["loc_id"]=> string(2) "93" ["org_id"]=> string(1) "9" ["naam"]=> string(12) "test" 
["Organisatie"]=> array(4) { ["org_id"]=> string(1) "9" ["naam"]=> string(3) "test"
["Sc"]=> array(1) { [0]=> array(6) { ["sc_id"]=> string(1) "1" ["sc_nummer"]=> NULL ["type"]=> string(6) "All-in" } } } 
["Sc"]=> array(0) { } 

}

The problem is the duplicate ["Sc"] key, because in php when an array has duplicate keys, that last key overwrites the first (what i read anyway), this makes it impossible to make a check like:

if(!empty($SC) or !empty($SC2) {}

I can't seem to figure out how to make doctrine name one of the keys differently or maybe even merge the two.

I tried using INDEXBY but that changed the key inside the ['SC'] array and not ['SC'] itself.

Any ideas?

A: 

You can rename it within the query. So in your example, that would only change on of the sc_id to something like

[...] sl.sc_id AS your_new_key [...]

See http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en#select-queries:aggregate-values

DrColossos
This actually worked, Thank you!
iggnition