Domanda

So che posso ciclo attraverso ogni livello dell'oggetto, ma vorrei un approccio più semplice a questo.

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
)

Ho bisogno di questo valore di ID di type_5, come avrei potuto ottenere questo in una soluzione semplice.

Alcuni altri punti a cui pensare:

  • Non lo so quanto grande o quanto in profondità l'oggetto di array andrò, potrebbe essere più o inferiore a 5

Ho sentito parlare di ricorsione, ma havn't trovato nulla Penso che potrei usare che mantiene semplice. Forse alcuni tutorial meglio mi avrebbe aiutato fuori. Se sapevo in quale parte della matrice di oggetti del valore che era in Neede potrei chiamare direttamente? qualcosa come: $ object [5] -> id ???

È stato utile?

Soluzione

Ecco come funziona ricorsione (in genere)

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

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

Si inizia con un ingresso, e ti dirà la funzione di continuare a chiamare se stessa finché non può restituire un output valido. Nel tuo caso, si potrebbe fare in questo modo:

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 ] );
    }
}

Ora, dal momento che si sta utilizzando un array per sobjects, si consiglia di sostituire la linea di sotto #controlla SOTTO, con il seguente:

$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;
    }
}

Altri suggerimenti

E 'molto semplice:

class SObject{
/*...*/

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

E si chiama

$id = $query_obj->getId();

Dump questo come un XML e usare XPath su di esso se avete bisogno di fare un sacco di queryes in questa struttura

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top