6 Replies Latest reply on Aug 13, 2013 11:25 PM by Malcolm

    Working with CWP RelatedSet Arrays

    Malcolm

      I have the code below. At the point that I call the getFields() method on the related set object I get a fatal error. I can't see why $rset is regarded as a non-object.

       

      $relatedsets = $layout->listRelatedSets();

      foreach ($relatedsets as $rs) {

      $rset = $record->getRelatedSet($rs);

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

      echo "Error: " . $rset->getMessage() . " ";

      exit;

      }

       

      $field_data = $rset->getFields();

      // PHP Fatal error: Call to a member function getFields() on a non-object

       

      }

       

      To confirm this the function is_object($rset) returns false but getRelatedSet is meant to return an FileMaker Record Object. It certainly looks like it when I use print_r($rset) to inspect the result visually I see this:

       

      Array

      (

      [0] => FileMaker_Record Object

      (

      [_impl] => FileMaker_Record_Implementation Object

       

      Any clues to where I am going wrong with this?

        • 1. Re: Working with CWP RelatedSet Arrays
          steve_ssh

          Hello Malcom,

           

          Apologies in advance if I am way off-base here, but, just by looking at this code, I would be expecting $record->getRelatedSet( $rs ) to be returning an array of FM Record objects, as opposed to an individual FM Record object.

           

          Very best,

           

          -steve

          • 2. Re: Working with CWP RelatedSet Arrays
            mbraendle

            To interpret Steve's answer, you may just write:

             

            foreach ($relatedsets as $rs) {

              $field_data = $rs->getFields();

            // further code

            }

            1 of 1 people found this helpful
            • 3. Re: Working with CWP RelatedSet Arrays
              Malcolm

              Hi Steve,

               

              You are right. I was expecting to be able to operate on the result rather than having to loop through the result and operate on the value of each array item.

               

              Given a layout which contains portals, this code will determine what they are and display the field values

               

              $listOfRelatedSets = $layout->listRelatedSets();

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

                   echo "Error: " . $listOfRelatedSets->getMessage() . "\n";

                   exit;

              }

               

              if ( ! empty($listOfRelatedSets) ) {

                   foreach ($listOfRelatedSets as $setName) {

                        $relatedSet = $record->getRelatedSet($setName);

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

                             echo "Error: " . $relatedSet->getMessage() . "\n";

                             exit;

                        }

                        $i = 0;

                        echo '';
              foreach($relatedSet as $relatedRecObject){
              $fieldNames = $relatedRecObject->getFields();
              foreach ($relatedRecObject as $obj => $relatedRecord) {
              echo "";
              foreach($fieldNames as $fieldName){
              echo "";
              }
              echo '';
              }
              }
              echo '

              '.$setName.'

              ".$i++."

              ".$relatedRecord->getField($fieldName). "

              ';

                   }

               

              • 4. Re: Working with CWP RelatedSet Arrays
                Malcolm

                Hi Martin,

                 

                That was what I expected to be able to do but I found it necessary to dig a level deeper. You can see the code in my response to Steve.

                 

                Malcolm

                • 5. Re: Working with CWP RelatedSet Arrays
                  Malcolm

                  Hi Steve,

                   

                  You are right. I was expecting to be able to operate on the result rather than having to loop through the result and operate on the value of each array item.

                   

                  Given a layout which contains portals, this code will determine what they are and display the field values

                   

                  $listOfRelatedSets = $layout->listRelatedSets();

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

                       echo "Error: " . $listOfRelatedSets->getMessage() . "\n";

                       exit;

                  }

                   

                  if ( ! empty($listOfRelatedSets) ) {

                       foreach ($listOfRelatedSets as $setName) {

                            $relatedSet = $record->getRelatedSet($setName);

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

                                 echo "Error: " . $relatedSet->getMessage() . "\n";

                                 exit;

                            }

                            $i = 0;

                            echo '';
                  foreach($relatedSet as $relatedRecObject){
                  $fieldNames = $relatedRecObject->getFields();
                  foreach ($relatedRecObject as $obj => $relatedRecord) {
                  echo "";
                  foreach($fieldNames as $fieldName){
                  echo "";
                  }
                  echo '';
                  }
                  }
                  echo '

                  '.$setName.'

                  ".$i++."

                  ".$relatedRecord->getField($fieldName). "

                  ';

                       }

                   

                  • 6. Re: Working with CWP RelatedSet Arrays
                    Malcolm

                    I've been studying the API documentation that ships with server the whole time and I've just discovered the key to the problem that I was having.

                     

                    I got a list of related set names from the Layout object. I then got the related sets from the Record object. The Record getRelatedSet function returns an array of Record objects. If I get the related sets from the Layout object it returns an array of Related Set objects.