How to go from results array of objects to array for csv?

Is there an easier way to go from a results object to a flatter array for a csv?

$this->loadModel('AdverseEvents.Queries');
    $results= $this->Queries->find('all')
            ->select([
                'Queries.crf_adverse_event_id','CountryLink.id','CountryData.name','Sites.name', 
                'CrfAdverseEvents.participant_id', 'CrfAdverseEvents.medical_terms',
                'CrfAdverseEvents.date_onset', 'Queries.type', 'Queries.title', 
                'Queries.status','Queries.message', 'Queries.created'])
            ->contain([
                'CrfAdverseEvents' => [    
                    'Sites' => [ 'CountryLink' => ['CountryData'] ]
                ]
            ])
            ->order(['Queries.created' => 'DESC'])
            ->all()
            ->toArray();

    $table = [];
    foreach ($results as $query) {
        $table[] = [
            $query->crf_adverse_event_id,
            $query->crf_adverse_event->site->country_link->country_data->name,
            $query->crf_adverse_event->site->name,
            $query->crf_adverse_event->participant_id,
            $this->escapeForCsv($query->crf_adverse_event->medical_terms),
            $query->crf_adverse_event->date_onset,
            $query->type,
            $this->escapeForCsv($query->title),
            $query->status,
            $this->escapeForCsv($query->message),
            $query->created->format('Y-m-d H:s:i')
        ];
    }

Use this package: https://github.com/FriendsOfCake/cakephp-csvview

1 Like

Is there a way using CakePHP’s hash functions?