0

オブジェクトの各レベルをループできることはわかっていますが、これにはもっと簡単なアプローチが必要です。

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
)

type_5のこのId値が必要ですが、簡単なソリューションでこれを取得するにはどうすればよいですか。

考慮すべき他のいくつかのポイント:

  • 配列のオブジェクトがどれだけ大きくなるか、どれだけ深くなるかはわかりませんが、5より大きくても小さくてもかまいません。

再帰について聞いたことがありますが、単純にするために使用できると思うものは何も見つかりませんでした。たぶん、いくつかのより良いチュートリアルが私を助けるでしょう。オブジェクトの配列のどの部分に必要な値があるかを知っていれば、それを直接呼び出すことができますか?次のようなもの:$ object [5]-> id ???

4

3 に答える 3

2

それは非常に簡単です:

class SObject{
/*...*/

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

そして、あなたは電話します

$id = $query_obj->getId();
于 2009-09-25T15:13:02.560 に答える
2

これが再帰の仕組みです(一般的に)

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

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

入力から開始し、有効な出力を返すことができるまで関数自体を呼び出し続けるように関数に指示します。あなたの場合、次のようにすることができます:

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

ここで、sobjects に配列を使用しているため、#SEE BELOW の下の行を次のように置き換えることができます。

$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;
    }
}
于 2009-09-25T17:14:59.137 に答える
1

この構造に対して多くのクエリを作成する必要がある場合は、これを XML としてダンプし、XPATH を使用します。

于 2009-09-25T16:59:23.630 に答える