[英]Lithium: How to manually populate a DocumentSet?
How can I return data from an external source as a DocumentSet? 如何从外部源作为DocumentSet返回数据?
I set up a custom data source to interface with Amazon's Product Advertising API. 我设置了一个自定义数据源来与Amazon的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 ). 为此,我将substantial
lithium\\data\\source\\Http
子类化,并按照文档( 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)
. 但是,我的锂版本(0.11,最新发行版)似乎没有示例中的强制转换方法,如果我创建一个强制转换方法,则当我
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
. 因此,我做了一个自定义
item
函数,通过调用parent::item
来创建文档,就像文档示例对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 .
然后,在递归调用之后,我得到一个Document对象数组,最后一次调用
parent::item
然后给了我一个空的DocumentSet对象 。 How should I pass the data on to create a proper DocumentSet?
我应该如何传递数据以创建正确的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);
}
I would track the master branch instead of the release versions. 我将跟踪master分支而不是发行版本。
In your case, since you're boxing your objects manually, I would do something like: 在您的情况下,由于您要手动装箱对象,因此我将执行以下操作:
return $this->_instance('set', compact('data'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.