[TYPO3-mvc] Best practice for ajax json_encode on repository of model

Ludwig Rafelsberger ludwig.rafelsberger at gmx.at
Mon Feb 18 20:35:58 CET 2013


Hi Dominic, 

> PHP
> $mailEntries = 
> $this->mailEntryRepository->getEmailsFromUser($this->user,
> $args['userId']); return json_encode( $mailEntries->toArray() );
> 
> This results into an empty json array.

When encoding your extbase objects, the native PHP function
json_encode() will not consider protected attributes - which you
probably are using. You might implement a simple "serialize" method
in your objects, producing a suitable (associative, nested) array. Or
have a look at the JsonSerializable interface*.

Cheers, 
Ludwig

[*]
http://www.php.net/manual/en/class.jsonserializable.php


More information about the TYPO3-project-typo3v4mvc mailing list