doctrine2 - Flow: Convert object to array for CSV export -
i want export object "mitglied" .csv-file. controller looks that:
public function exportaction() { // find mitglieds $records = $this->mitgliedrepository->findtennis(); // set path export-file $csvpath = '/var/www/apps/flow/packages/application/itoop.atc/resources/private/export/test.csv'; $fp = fopen($csvpath, 'w'); foreach ($records $lines) { fputcsv($fp, $lines); } fclose($fp); }
when call exportaction, error:
#1: warning: fputcsv() expects parameter 2 array, object given in /var/www/apps/flow/data/temporary/development/cache/code/flow_object_classes/itoop_atc_controller_mitgliedcontroller.php line 494
line 494 is...
fputcsv($fp, $lines);
...so think have convert object "mitglied" array.
my public function findtennis
in mitgliedrepository looks that:
public function findtennis() { $query = $this->createquery(); $result = $query->matching($query->equals('abteilung', 'tennis')) ->setorderings(array('name' => \typo3\flow\persistence\queryinterface::order_ascending)) ->execute(); return $result; }
i tried set toarray(); in repository that:
public function findtennis() { $query = $this->createquery(); $result = $query->matching($query->equals('abteilung', 'tennis')) ->setorderings(array('name' => \typo3\flow\persistence\queryinterface::order_ascending)) ->execute() ->toarray; return $result; }
but following error:
#1: notice: undefined property: typo3\flow\persistence\doctrine\queryresult::$toarray in /var/www/apps/flow/data/temporary/development/cache/code/flow_object_classes/itoop_atc_domain_repository_mitgliedrepository.php line 105
line 105 of course is
->toarray;
does know, how convert object array in flow?
with following example export works, think (formatting of the) repository query problem.
public function exportaction() { // set path export-file $csvpath = '/var/www/apps/flow/packages/application/itoop.atc/resources/private/export/test.csv'; $test = array ( array('xxx', 'bbb', 'ccc', 'dddd'), array('123', '456', '789'), array('aaa', 'bbb') ); $fp = fopen($csvpath, 'w'); foreach ($test $lines) { fputcsv($fp, $lines); } fclose($fp);
}
please point me right direction. thank you!
the error messages explained
#1: warning: fputcsv() expects parameter 2 array, object given in /var/www/apps/flow/data/temporary/development/cache/code/flow_object_classes/itoop_atc_controller_mitgliedcontroller.php line 494
fputcsv
expects it's 2nd parameter array. array written csv line single file, each array element column. when iterating on $records
variable, instances of domain object class (so sth. itoop\atc\domain\model\mitglied
). that's undefined behaviour, warning.
#1: notice: undefined property: typo3\flow\persistence\doctrine\queryresult::$toarray in /var/www/apps/flow/data/temporary/development/cache/code/flow_object_classes/itoop_atc_domain_repository_mitgliedrepository.php line 105
toarray
function offered doctrine queryresult
class. typically, doctrine queries not fetch objects returned query, return iterator fetches , maps entities on-demand. toarray
method fetches records @ once , returns array instead of iterator. error occurs, because try access toarray
as property, , not calling method. following code correct:
$result = $query->matching($query->equals('abteilung', 'tennis')) ->setorderings(array('name' => \typo3\flow\persistence\queryinterface::order_ascending)) ->execute() ->toarray(); // <- mind brackets!
however, not anything, because in controller, still iterating on list of domain entities (foreach
not care if iterating on iterator or array; that's point of iterators in php).
quick&dirty solution
convert domain entities hand in controller. only can know how csv export should like, cannot automated. i'm thinking this:
foreach ($records $record) { $csvline = [ $record->getfirstproperty(), $record->getsecondproperty(), // , on... ]; fputcsv($fp, $csvline); }
better solution
rendering csv data not concern should addressed in controller. basically, should go view. can implement a custom view class handling csv output.
for that, need implement \typo3\flow\mvc\view\viewinterface
. easiest way subclass \typo3\flow\mvc\view\abstractview
. name view class <packagenamespace>\view\<controller>\action<format>
(so sth. itoop\atc\view\mitglied\exportcsv
. implement csv export logic in view's render()
method. flow pick , use view class automatically it's present.
implementing custom views explained in depth in this article -- it's in german though, although based on class naming suspect won't problem ;).
Comments
Post a Comment