You are here:  » Using Magic Parser with CakePHP

Using Magic Parser with CakePHP

Submitted by Light and Shadow on Wed, 2008-02-06 02:55 in

I'm trying to use Magic Parser to parse a complex Soap request. Amazingly, I was able to paste the data into the website and it figured out the schema. I was sold :-)

However, I'm currently trying to integrate Magic Parser in a CakePHP model. During this process, I've run across the following questions..

01. How do I know when all records have been parsed? I need to return an associated array that build as each record is appended.

02. How can I specify an instance method of a class as the callback for each record?


- Scott

Submitted by support on Wed, 2008-02-06 08:08

Hello Scott,

Thank you for your comments! To answer your questions;

1) You will know when parsing has completed because the call to MagicParser_parse() will return. Until it has processed the entire file no further PHP code in the script will be executed, other than the code within the specified record handler function for that parse.

2) Unfortunately it is not possible to specify a class member as the record handler function; however this is not normally a problem because (as per the previous answer), Magic Parser does not return until the parse has completed. Therefore, you can simply specify a regular, non-class function that loads the data into a global array; and then following the parse copy that global array into a local class variable.....

Here's what your record handler might look like (external to your class):

  function myRecordHandler($record)
    global $records;
    // add the current record to the global $records array
    $records[] = $record;

...and then within your class:

  global $records;
  $records = array();
  $this->records = $records;

Hope this helps!

Submitted by jorrit on Wed, 2013-08-21 11:18

The solution of a global array with records seemed not an option for me, because it would explode server memory. As an alternative, I use this approach:

function myRecordHandler($record)
  global $currentObject;

...and then within my factory class:
global $currentObject;
$currentObject = $this;
$currentObject = NULL;

... and thirdly in my (inherited) class:
public function updateRecord($record)

What do you think of this approach?

Jorrit Steetskamp

Submitted by support on Wed, 2013-08-21 12:00

Looks fine Jorrit but if you're not sure at all or doesn't seem to work as expected just let me know...