views:

161

answers:

3

I know I can loop through each level of the object, but I would like a more simple approach to this.

QueryResult Object
(
    [queryLocator] => 
    [done] => 1
    [records] => Array
        (
            [0] => SObject Object
                (
                    [type] => type_1
                    [fields] => 
                    [sobjects] => Array
                        (
                            [0] => SObject Object
                                (
                                    [type] => type_2
                                    [fields] => 
                                    [sobjects] => Array
                                        (
                                            [0] => SObject Object
                                                (
                                                    [type] => type_3
                                                    [fields] => 
                                                    [sobjects] => Array
                                                        (
                                                            [0] => SObject Object
                                                                (
                                                                    [type] => type_4
                                                                    [fields] => 
                                                                    [sobjects] => Array
                                                                        (
                                                                            [0] => SObject Object
                                                                                (
                                                                                    [type] => type_5
                                                                                    [fields] => 
                                                                                    [Id] => 12345_I_need_this
                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [size] => 1
)

I need this Id value of type_5, how could I get this in a simple solution.

Some other points to think about:

  • I will not know how big or how deep the object of arrays will go, could be more or less than 5

I have heard about recursion but havn't found anything I think I could use that keeps it simple. Maybe some better tutorials would help me out. If I did know in what part of the array of object the value I neede was in could I just call it directly? something like: $object[5]->id ???

+2  A: 

It's very simple:

class SObject{
/*...*/

    public getId(){
        if(isset($this->Id)){
            return $this->Id;
        } else {
            return $this->sobjects[0]->getId();
        }
    }
}

And you call

$id = $query_obj->getId();
erenon
SObject is already a class. I am using this but how can I make if simple: $queryResults->records[0]->sobjects[0]->sobjects[0]->sobjects[0]->sobjects[0]->Id
Phill Pafford
@Phill: you have to extend the used class with the function getId described above. Are you able to modify the sobject class?
erenon
@Phill: You would call $queryResults->records[0]->getId();
Aistina
+1  A: 

Dump this as an XML and use XPATH on it if you need to make lots of queryes on this structure

Quamis
Clever.
Christopher W. Allen-Poole
+2  A: 

Here is how recursion works (generally)

function recursiveFunctionName( input ) // returns value;
{
    //Do something to input to make it new_input

    if( isSomethingAccomplished )
    {
        return value;
    }
    else
    {
        return recursiveFunctionName( new_input );
    }
}

You start with an input, and you tell the function to continue to call itself until it can return a valid output. In your case, you could do it this way:

function getID( SObject $so )
{
    // equates to isSomethingAccomplished...  You have found the value
    // you want returned, so pass that out.
    if( $so->id )
    {
        return $so->id;
    }
    else
    {
        // otherwise, this will return the value from the next level, 
        // pass that out.
        # SEE BELOW FOR ONE MORE NOTE HERE.
        return getID( $so->sobjects[ 0 ] );
    }
}

Now, since you are using an Array for sobjects, you may want to replace the line below #SEE BELOW, with the following:

$objs  = $so->sobjects;
$count = count( $objs );

// Iterate through all of its children, testing each of the child nodes.
// (You're actually using iteration and recursion in combination here).
for( $i = 0; $i < $count; $i++ )
{
    $curr = getID( $objs[ $i ] );

    // This is the same test as above.
    if( $curr )
    {
        return $curr;
    }
}
Christopher W. Allen-Poole
Thanks this really helped
Phill Pafford