views:

37

answers:

2

Hi,

I have a class that is query data via a SOAP client. the results of the query are returned as an array of objects. Something strange is happening. I'm not sure if it only occurs when there is one element in the array.

Here is the code:

function getNotes($ticketid){

    $ticketid = $this->ms_escape_string($ticketid);

    $conn = new Connections();
    $client = $conn->connectToAPI();

    $xml = array('sXML' =>"<queryxml>".
                                "<entity>TimeEntry</entity>".
                                "<query>".
                                    "<field>TicketID".
                                        "<expression op='equals'>$ticketid</expression>".
                                    "</field>".
                                "</query>".
                        "</queryxml>");

    $result = $client->query($xml);

    $noteso = $result->queryResult->EntityResults->Entity;

    //set array counter
    $x=0;


    var_dump($noteso);

        foreach($noteso as $note){

            var_dump($note->ResourceID);

            $name = $this->getResourceName($note->ResourceID);


            $timeentryarray[$x] = array('createdate'     => $note->CreateDateTime,
                            'summarynotes'  => $note->SummaryNotes,
                            'resource'    => $name
                           );
            $x++;

        }

        return $timeentryarray;
}

when the application run the var_dump($noteso) is fine. Prints out

object(stdClass)#12 (15) {
    ["id"]=> int(36988)
    ["UserDefinedFields"]=> object(stdClass)#13 (0) { } 
    ["TicketID"]=> int(26390) 
    ["InternalAllocationCodeID"]=> int(28303142) 
    ["Date"]=> string(19) "2010-10-25T00:00:00" 
    ["StartDateTime"]=> string(19) "2010-10-25T04:59:00" 
    ["EndDateTime"]=> string(19) "2010-10-25T05:04:00" 
    ["HoursWorked"]=> float(0.08)
    ["HoursToBill"]=> float(0.0833333358) 
    ["OffsetHours"]=> float(0)
    ["SummaryNotes"]=> string(69) "Hi Sarah, Do you have an example of a bounced email? Regards Simon" 
    ["InternalNotes"]=> string(0) "" 
    ["RoleID"]=> int(24482927) 
    ["CreateDateTime"]=> string(23) "2010-10-25T05:06:28.023" 
    ["ResourceID"]=> int(29699970) 
} 

but the second var_dump($note->ResourceID) in the foreach loop only shows

NULL

then each line that accesses the array after that throws an error:

Notice: Trying to get property of non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ClientPortal\classes\OpenTickets.php on line 204

Line 204 being

$name = $this->getResourceName($note->ResourceID);

Been trying to debug this one for a while, can someone offer any suggestions?

Regards,

Jonesy

+2  A: 

If I'm not mistaken, you are going about this the wrong way. $noteso has a number of properties, among them ResourceID.

The foreach is running through every property of that object. Those properties will not have a property named ResourceID.

It looks like what you should be doing is drop the foreach and address $noteso->ResourceID.

Pekka
what seems to be happening is that if theree's more than one result it's creating an array of objects, but if there is one result it is simply returning an object. I was able to check this by doing an if count of array is more than one access objects via a foreach loop and else access object without foreach
iamjonesy
@iamjonesy in that case you'll have to accommodate for the possibility, e.g. using `is_array()`.
Pekka
@iamjonesy alternatively turn it into an array if it's not one: `if (!is_array($noteso)) $noteso = array($noteso);`
Pekka
thanks that helps me a lot!
iamjonesy
`$noteso = (array)$noteso;` would be shorter version of your typecasting.
RobertPitt
@Robert that would turn `$noteso` into an associative array if it's only one object, wouldn't it?
Pekka
yes, as a matter of a fact it would. I was thinking of the length of code required and misinterpreted what you was actually doing.
RobertPitt
+1  A: 

It looks like $noteso = $result->queryResult->EntityResults->Entity; is return a single object,

Maybe a while loop is needed, what soap-client are you using ?

RobertPitt
thanks for your reply, please see my response to @Pekka
iamjonesy