Pregunta

Sé que puede recorrer cada nivel del objeto, pero me gustaría un enfoque más simple a este.

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
)

Necesito este valor Id de type_5, cómo podría conseguir esto en una solución sencilla.

Algunos otros puntos a considerar:

  • No voy a saber el tamaño o la profundidad del objeto de las matrices a ir, podría ser más o menos del 5

He oído hablar de la recursividad, pero havn't encontrado nada creo que podría utilizar que mantiene simple. Tal vez algunos tutoriales mejor que me ayudarían a cabo. Si yo sabía en qué parte de la matriz de objeto el valor que estaba en NEEDE podría simplemente llamar directamente? algo como: $ objeto [5] -> Identificación ???

¿Fue útil?

Solución

Aquí es cómo funciona la recursividad (general)

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

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

Se empieza con una entrada, y le dirá la función de continuar a llamarse a sí mismo hasta que pueda devolver una salida válida. En su caso, podría hacerlo de esta manera:

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

Ahora, dado que está utilizando una matriz de sobjects, es posible que desee reemplazar la línea de abajo #SEE abajo, con lo siguiente:

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

Otros consejos

Es muy simple:

class SObject{
/*...*/

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

Y se llama

$id = $query_obj->getId();

Dump esto como un XML y utilizar XPATH en él si tiene que hacer un montón de queryes sobre esta estructura

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top