在PHP中从数组中获取obj

时间:2018-04-25 17:31:17

标签: php arrays object salesforce soql

我正在尝试使用SalesForce API查询的以下响应。我似乎无法弄清楚如何捕获有关[任务] obj的任何信息。用PHP。请参阅下面的API响应&现有代码。 如下所示,我可以使用帐户信息,但不能使用任务信息。

$query ="SELECT accountId,Status,Id,Service_Account_DMKT__r.name,(select id,Activity_Type__c,ActivityDate from Case.Tasks) from case where Service_Account_DMKT__r.name='budcrossfordfd' AND Owner.Name ='".$name."' AND CreatedDate = THIS_MONTH AND Record_type_name__c='Performance Reviews' AND status NOT IN ('')";
$response = $mySforceConnection->query($query);
$queryResult = new QueryResult($response); //We collected the response from our SF Query
$counter=0;    
for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
  $counter++;
  $accountId = $queryResult->current()->AccountId; //Pulls accountId from Query
  $accountName=$queryResult->current()->Service_Account_DMKT__r->Name; //pulls the name of the account the case exists on, note that when we use XXX__r.XXX, we sep. this in PHP with ->
  $caseId= $queryResult->current()->Id; //Pulls CaseId from Query
  $taskType=$queryResult->current()->Tasks->Activity_Type__c; //Pulls the Type of the Task that exists under the Case (This isn't working!)
  $taskId= $queryResult->current()->Tasks->Id; //Pulls Task Id (This isn't Working!)
  $taskDate=$queryResult->current()->Tasks->Activity_Date; //Pulls date the Task was Created (This isn't Working!)
  echo'<p>AccountId=' . $accountId . '</p>';
  echo'<p>accountName=' . $accountName . '</p>';
  echo'<p>caseId=' . $caseId . '</p>';
  echo'<p>taskType=' . $taskType . '</p>';
  echo'<p>taskId=' . $taskId . '</p>';
  echo'<p>taskDate=' . $taskDate . '</p>';
}

回应:

Notice:  Trying to get property of non-object in Z:\web\phpToolKit\test\partner.php on line 65



Notice:  Trying to get property of non-object in Z:\web\phpToolKit\test\partner.php on line 66



Notice:  Trying to get property of non-object in Z:\web\phpToolKit\test\partner.php on line 67

AccountId=0016000000NsN3uAAF

accountName=budcrossfordfd

caseId=5000e00001J7KtpAAF

taskType=

taskId=

taskDate=

QueryResult Object
(
    [queryLocator] => 
    [done] => 1
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => Case
                    [Id] => Array
                        (
                            [0] => 5000e00001J7KtpAAF
                            [1] => 5000e00001J7KtpAAF
                        )

                    [any] => Array
                        (
                            [0] => 0016000000NsN3uAAFClosed - Approved5000e00001J7KtpAAF
                            [Service_Account_DMKT__r] => stdClass Object
                                (
                                    [type] => Account
                                    [Id] => 
                                    [any] => budcrossfordfd
                                )

                            [Tasks] => stdClass Object
                                (
                                    [done] => 1
                                    [queryLocator] => 
                                    [records] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [type] => Task
                                                    [Id] => Array
                                                        (
                                                            [0] => 00T0e00006GH5Q2EAL
                                                            [1] => 00T0e00006GH5Q2EAL
                                                        )

                                                    [any] => Call - Outbound2018-04-04
                                                )

                                            [1] => stdClass Object
                                                (
                                                    [type] => Task
                                                    [Id] => Array
                                                        (
                                                            [0] => 00T0e00006GGNgEEAX
                                                            [1] => 00T0e00006GGNgEEAX
                                                        )

                                                    [any] => 
                                                )

                                        )

                                    [size] => 2
                                )

                        )

                )

        )

    [size] => 1
    [pointer] => 1
    [sf:QueryResult:private] => 
)

我也尝试过:

foreach($queryResult->records as $case){     
  $accountId = $queryResult->current()->AccountId; //Pulls accountId from Query
  $accountName=$queryResult->current()->Service_Account_DMKT__r->Name; //pulls the name of the account the case exists on, note that when we use XXX__r.XXX, we sep. this in PHP with ->
  $caseId= $queryResult->current()->Id; //Pulls CaseId from Query     
  echo'<p>AccountId=' . $accountId . '</p>';
  echo'<p>accountName=' . $accountName . '</p>';
  echo'<p>caseId=' . $caseId . '</p>';     

  foreach($case->Tasks->records as $record) {
   $taskId=$record->Id;
   $taskType=$record->Activity_Type__c;
   $taskDate=$record->ActivityDate;
   echo'<p>taskId=' . $taskId . '</p>';
   echo'<p>taskType=' . $taskType . '</p>';
   echo'<p>taskDate=' . $taskDate . '</p>';
  }
  print_r($case);

返回

AccountId=0016000000NsN3uAAF

accountName=budcrossfordfd

caseId=5000e00001J7KtpAAF



Notice:  Undefined property: stdClass::$Tasks in Z:\web\phpToolKit\test\partner.php on line 69



Notice:  Trying to get property of non-object in Z:\web\phpToolKit\test\partner.php on line 69



Warning:  Invalid argument supplied for foreach() in Z:\web\phpToolKit\test\partner.php on line 69

它看起来像&#34;任务&#34;不是obj的名字,但我无法弄清楚它会是什么! 我用过:Salesforce PHP Toolkit - pullup data from nested SOQL query 我的第二个例子

1 个答案:

答案 0 :(得分:0)

我使用了Sammitch的答案并将代码更新为:

foreach($queryResult->records as $case){     
  $accountId = $queryResult->current()->AccountId; //Pulls accountId from Query
  $accountName=$queryResult->current()->Service_Account_DMKT__r->Name; //pulls the name of the account the case exists on, note that when we use XXX__r.XXX, we sep. this in PHP with ->
  $caseId= $queryResult->current()->Id; //Pulls CaseId from Query     
  echo'<p>AccountId=' . $accountId . '</p>';
  echo'<p>accountName=' . $accountName . '</p>';
  echo'<p>caseId=' . $caseId . '</p>';     

  foreach($case->any['Tasks']->records as $record) {
   $taskId=$record->Id[0]; //This correctly returns the first ID in the array
   //$taskType=$record->Activity_Type__c; //This doesn't work because this information  is located in a different sub obj "any"
   //$taskDate=$record->ActivityDate; //This also doesn't work because it's located in a sub object "any"
   echo'<p>taskId=' . $taskId . '</p>';
  echo '<p>' . $record->any . '</p>' //watch out, this object might look to only be a string, but there is a lot of white space info in here. you can see it by wrapping it in json_encode($record->any).
  }
  print_r($case);

我使用了上面的代码,然后通过使用strpos来询问obj是否包含特定字符串,我能够将字符串与它进行比较。这可能会令人困惑,所以要小心!