[TYPO3-mvc] How to transform objects to array
Xavier Perseguers
xavier at typo3.org
Tue Jun 25 08:10:00 CEST 2013
Hello Stefan,
Stefan Kruse wrote:
>> Your problem is not clear. You have objects, meaning you have an array
>> of objects and the foreach loop works but you cannot access properties
>> of your objects or the foreach loop itself does not work?
>
> maybe i missunderstood you. I try to better explain. I get a result
> something like this:
>
> object(Apache_Solr_Response)#999 (5) {
> ["_response":protected]=>
> object(Apache_Solr_HttpTransport_Response)#1000 (5) {
>
> ["_statusCode":"Apache_Solr_HttpTransport_Response":private]=> int(200)
>
> ["_statusMessage":"Apache_Solr_HttpTransport_Response":private]=> string(2)
> "OK"
> [...]
>
> And so on. When i now overgive this result on a forech loop to iterate
> through, I get the error message that the foreach only accepts arrays and
> objects which implemented IteratorInterface. That's why I need to transform
> my result to array. But maybe theres a better solution.
OK, we are getting closer to a description of what you try to achieve,
so you have a Apache_Solr_Response [1] that you pass over to your view
and but that is not clear what you try to iterate over. Looking at [1] I
guess you try to iterate over the result of the various getters:
- getHttpStatus()
- getHttpStatusMessage()
- getType()
- getEncoding()
- getRawResponse()
so basically, for Extbase being able to extract the info, you need to
iterate over properties
- httpStatus
- httpStatusMessage
- type
- encoding
- rawResponse
Something you may do like that:
<ul>
<f:for each="{0:'httpStatus', 1:'httpStatusMessage', 2:'type',
3:'encoding', 4:'rawResponse'}" as="property">
<li>{property} = {myApacheSolrResponse.{property}}</li>
</f:for>
</ul>
But it won't work as {property} cannot be nested. You may write your own
ViewHelper
<my:fetchProperty object="{myApacheSolrResponse}" property="{property}"/>
Or write an class that add Iterator over properties. Something like that
(did not test):
class EntityIterator implements Countable, IteratorAggregate {
protected $object = NULL;
public function __construct($object) {
$this->object = $object;
}
public function toArray() {
static $fields = NULL;
if ($fields === NULL) {
$fields = array();
$class = new ReflectionClass($this->object);
$properties = array_keys($class->getDefaultProperties());
foreach ($properties as $property) {
$p = $class->getProperty($property);
$p->setAccessible(TRUE);
$value = $p->getValue($this->object);
if (!is_object($value)) {
$fields[$property] = $value;
}
}
}
return $fields;
}
public function getIterator() {
return new ArrayIterator($this->toArray());
}
public function count() {
return count($this->getIterator());
}
}
(note: in general you should try to access corresponding getter instead
of reading the property)
and then encapsulate your Solr response in it:
$myResponse = new EntityIterator($mySolrResponse);
$this->view->assign('response', $myResponse);
and in your view:
<ul>
<f:for each="{response}" key="property" value="value">
<li>{property} = {value}</li>
</f:for>
</ul>
should now work.
HTH
[1]
http://code.google.com/p/solr-php-client/source/browse/trunk/Apache/Solr/Response.php?r=54
--
Xavier Perseguers
Release Manager TYPO3 4.6
TYPO3 .... inspiring people to share!
Get involved: http://typo3.org
More information about the TYPO3-project-typo3v4mvc
mailing list