Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Ramy NATOUR
    [Knowledge Foundation API && Connect PHP API] Getting...Answered
    Topic posted July 1, 2015 by Ramy NATOURSilver Medal: 2,000+ Points, last edited July 2, 2015, tagged Connect PHP 
    360 Views, 1 Comment
    Title:
    [Knowledge Foundation API && Connect PHP API] Getting objects containing only NULL fields
    Content:

    Hello everyone,



    I'm currently facing a strange behaviour on both Knowledge Foundation API and Connect PHP API : Values returned by GetSmartAssistantSearch/SearchContent/Fetch are Objects containing only null fields.



    Here is a var_dump of a fetched account for example :





    ["Account"]=>
    object(RightNow\Connect\v1_2\Account)#22 (25) {
    ["ID"]=>
    NULL
    ["LookupName"]=>
    NULL
    ["CreatedTime"]=>
    NULL
    /* [...] */
    ["PasswordExpirationTime"]=>
    NULL
    ["Phones"]=>
    NULL
    ["Profile"]=>
    NULL
    ["SalesSettings"]=>
    NULL
    ["ServiceSettings"]=>
    NULL
    ["Signature"]=>
    NULL
    ["StaffGroup"]=>
    NULL
    }



    This behaviour seems strange to me because :



    • I called the InitConnectAPI() function after importing connect_init.phph file. I tried with both no args and with username + password
    • In case I give an incorrect account ID to the RNCPHP\Contact::fetch() function, it returns an error . Assuming this, he manages to find the searched contact when i supply the good ID but all its fields are NULL.
    • Not a single Exception is thrown nor a single error occurs (E_ALL is on)
    • All the needed permissions for the profile's account used are set to true
    • Getting those objects via ROQL(Object Explorer or Soap) returns the correct object with the correct values in the fields


    The above example treats only of the Connect PHP API behavior but I have more or less the same issue on the Knowledge Foundation API described in this post.



    Error(!= Warning) on the read-only init.php file:



    A call to the error_get_last() function after the fetches says that there is an error in the init.php read-only core's file  :




    Array
    (
    [type] => 8
    [message] => Undefined index: REDIRECT_URL
    [file] => /cgi-bin/siebeltocloud_fr.cfg/scripts/cp/core/framework/3.2.4/init.php
    [line] => 246
    )


     



    Questions:



    Assuming this issue touches both the Knowledge Foundation API and the Connect PHP API and behaves in a very close way:



    • Am I missing a configuration/initialization step (giving the permissions to the account used, calling InitConnectAPI(acc_usr, acc_pwd) )?
    • After finding the fetched contact, what step in the fields population could go wrong ?
    • Is the error coming from the init.php file the cause of this issue ? If yes, how can this issue be solved since the file is read-only and in the core (which I don't want to edit) ?
    • Is the field populating function from the Knowledge API and Connect PHP API the same ? How could it fail and what can be done to solve this ?


    If anyone has faced the same issue, giving a workaround/clues to solve it would be very kind.



    Thanks in advance,



    Have a nice day.



     



    Edit:



    • Tried using the function RNCPHP\ROQL::queryObject() and getting an empty result set as well
    Version:
    November 2014 Release
    Code Snippet:

    Best Comment

    Ramy NATOUR

    Problem Solved. The object not being populated on fetch is caused by lazy loading.

    I thought var_dump would cause the loading to happen since it iterates through all fields to dump them but it wasn't the case.

    The lazy loading behaviour is described in an undersection of the documentation : 'Connect PHP API'  chapter => 'Lazy loading'.

    In few words, you have to do a call on the attribute in order to get it initialized (i.e. '$account->LookupName;' will retrieve the needed values).

    You can industrialize the fields instanciation using reflection and calling all the fields on fetch.

    Thanks to Scott Harwell for his answer on stackoverflow,

    Edit: Using reflection, you can populate all fields as advised in the answer I got.

    Here is a script which illustrates how to do it:

    $fetched_acct = RNCPHP\Account::fetch($acct_id); $reflected = new \ReflectionClass($fetched_acct); $acct_attributes = $reflected->getProperties(); foreach ($properties as $property) { $property->getValue($fetched_acct); } /* Dumping to see if it got populated */ var_dump($fetched_acct);
    

    Comment

     

    • Ramy NATOUR

      Problem Solved. The object not being populated on fetch is caused by lazy loading.

      I thought var_dump would cause the loading to happen since it iterates through all fields to dump them but it wasn't the case.

      The lazy loading behaviour is described in an undersection of the documentation : 'Connect PHP API'  chapter => 'Lazy loading'.

      In few words, you have to do a call on the attribute in order to get it initialized (i.e. '$account->LookupName;' will retrieve the needed values).

      You can industrialize the fields instanciation using reflection and calling all the fields on fetch.

      Thanks to Scott Harwell for his answer on stackoverflow,

      Edit: Using reflection, you can populate all fields as advised in the answer I got.

      Here is a script which illustrates how to do it:

      $fetched_acct = RNCPHP\Account::fetch($acct_id); $reflected = new \ReflectionClass($fetched_acct); $acct_attributes = $reflected->getProperties(); foreach ($properties as $property) { $property->getValue($fetched_acct); } /* Dumping to see if it got populated */ var_dump($fetched_acct);