1 Reply Latest reply on Jul 13, 2012 2:28 PM by MattiasThorslund

    Missing error code in PHP API

    MattiasThorslund

      Summary

      Missing error code in PHP API

      Product

      FileMaker Server

      Version

      11, 12

      Operating system version

      Windows

      Description of the issue

      I have a PHP script that retrieves and displays a record from a layout on the FM server, and optionally displays the related data as well.

      My trouble is that FileMaker_Record::getRelatedSet() returns an unspecified error if the record I have retrieved has no related data. I can trap the error with FileMaker::isError() of course, but since the error code is not set, I cannot distinguish this NORMAL situation from actual errors.

      I made a workaround by hacking the FM PHP API, adding a unique error code at the point where this situation happens, but this means that my application won't work with the "stock" FM PHP API.

      I have compared the PHP API code that comes with both FM 11 and 12, and the latter version has no difference in this regard.

      Being new to FileMaker and the PHP API, perhaps there is some other way I should have gone about this problem, but there you go.

      Steps to reproduce the problem

      1: In FileMaker, create a layout with a portal to a related table.

      2: Makes sure that some of the records in the main table do NOT have related records.

      3: With the FM API, create a PHP file with the following code (replace the FM_* constants to specific server, file, layout names):

      <?php
      require 'FileMaker.php';

      $fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);

      $find = $fm->newFindAllCommand(FM_LAYOUT);
      $find->setRange(0, 10); //if there are too many records
      $find_result = $find->execute();

      if (FileMaker::isError($find_result)) {
          die("ERROR: code=".
              $find_result->getCode()."\n".
              $find_result->getMessage()
          );
      }

      $related_sets = $find_result->getRelatedSets();

      foreach($find_result->getRecords() as $record){

          //[process record data]

          foreach($related_sets as $related_set){
              $related_records = $record->getRelatedSet($related_set);

              if (FileMaker::isError($related_records)) {

                  //causes message 'ERROR: code= message=Related set "[table_name]" not present.'
                  die("ERROR: code=".
                      $related_records->getCode()."\n ".
                      'message='.$related_records->getMessage()
                  );
              } else {               
                  if(count($related_records) > 0){
                      //[process related records]
                  }
              }
          }
      }

      Expected result

      I would have expected the API to have a way to trap the error message that was triggered by the missing related data set.

      Actual result

      'Related set "[table_name]" not present.'

      Because the table name is included in the error message, I would have to use a regex to trap for it. Since the absence of related records is a NORMAL situation, the regex could be executed many times and could cause a performance hit.

      Exact text of any error message(s) that appear

      ERROR: code= message=Related set "[table_name]" not present.

      //Obviously, substitute the actual (related) table name for [table_name]

      Workaround

      I hacked the following API files, adding a unique error message for this condition:

      FileMaker/Implementation/RecordImpl.php (2 susbstitutions):

      - $Vcb5e100e = new FileMaker_Error($this->_fm, 'Related set "' . $Vaca007a7 . '" not present.');
      + $Vcb5e100e = new FileMaker_Error($this->_fm, 'Related set "' . $Vaca007a7 . '" not present.', 9001);

      inc/FileMaker/Error/en.php:
      -     1507 => 'Unable to log in to the SMTP server'
      +     1507 => 'Unable to log in to the SMTP server',
      +     9001 => 'Related set not present',

      Then, I changed the related sets "foreach" loop in the script above:

          foreach($related_sets as $related_set){
              $related_records = $record->getRelatedSet($related_set);

              if (FileMaker::isError($related_records)) {
                  if(9001 == $related_records->getCode()){
                      continue;
                  }

                  die("ERROR: code=".
                      $related_records->getCode()."\n".
                      'message='.$related_records->getMessage()
                  );
              } else {               
                  if(count($related_records) > 0){
                      //[process related records]
                  }
              }
          }