Question

How can I return data from an external source as a DocumentSet?

I set up a custom data source to interface with Amazon's Product Advertising API. To do this, I subclassed lithium\data\source\Http and redefined the read method to suit my needs as described in the documentation (http://li3.me/docs/manual/working-with-data/creating-data-sources.wiki).

However, my lithium version (0.11, last release) does not seem to have a cast method like in the example and if I create one it won't get called when I do return $this->item($model, $data, $options).

So, I made a custom item function to create the Documents by calling parent::item just like the documentation example does for cast. Then, after the recursive calls, I end up with an array of Document objects and the final call to parent::item then gives me an empty DocumentSet object. How should I pass the data on to create a proper DocumentSet?

Here's a minimal example of my code:

// Within class Amazon extends \lithium\data\source\Http

protected function _init() {
    // Define entity classes.
    $this->_classes += array(
        'entity' => 'lithium\data\entity\Document',
        'set' => 'lithium\data\collection\DocumentSet'
    );

    parent::_init();
}

public function read($query, array $options = array()) {
    // Extract from query object.
    $parameters = $query->export($this, array('keys' => array('conditions')));
    $conditions = $parameters['conditions'];

    // Code stripped to validate conditions and prepare Amazon request (that part works).
    // results in a $queryString variable.

    // Get response from Server.
    $xml = simplexml_load_string($this->connection->get($this->_config['basePath'], $queryString));

    // Stripped response validation and reformatting -> $items contains an array of SimpleXMLElement objects.

    return $this->item($query->model(), $items, array('class' => 'set'));
}

public function item($model, array $data = array(), array $options = array()) {
    // Recursively create Documents for arrays.
    foreach($data as $key => $value) {
        if(is_array($value)) {
            $data[$key] = $this->item($model, $value, array('class' => 'entity'));
        }
        else if(is_object($value) && get_class($value) == "SimpleXMLElement") {
            // Stripped code to extract data from XML object and put it in array $docData.

            $data[$key] = $this->item($model, $docData, array('class' => 'entity'));
        }
    }
    // Works perfectly for every (recursive) call with $options['class'] == 'entity' but fails for the final call with $options['class'] == 'set' (for this final call $data contains an array of Document objects).
    return parent::item($model, $data, $options);
}
Was it helpful?

Solution

I would track the master branch instead of the release versions.

In your case, since you're boxing your objects manually, I would do something like:

return $this->_instance('set', compact('data'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top