2

I'm working on exporting from a database table using Doctrine and symfony. I've made a call to my database with

$variableTable = Doctrine::getTable('Table Name')->findAll();

then I set another variable to get data from it like so:

$variable = $variableTable->getData();

I just want to be able to loop through that so I can pull the data I need to put into a csv file but I'm stuck on that part. For some reason, when I debug, it shows that the second variable that is getting set to the value of the getData is null/0.

I'm not sure if I'm missing something.

2 Answers 2

11

I think maybe you are trying to call getData on the collection instead of calling getData on the record... But that doesnt make much sense because you are still going to have to call getData on each record, so you might as well just loop over the collection. Additionally its possible there were no results from your findAll for some crazy reason so you should take that into account i think. Anyhow code similar to the following has worked for me in the past so without the details of your code its the best i can offer:

$records = Doctrine::getTable('RecordName')->findAll();

if($records->count()) {
   $csvPath = '/path/to/csv/file.csv';

   $csvh = fopen($csvPath, 'w');
   $d = ','; // this is the default but i like to be explicit
   $e = '"'; // this is the default but i like to be explicit

   foreach($records as $record) {
       $data = $record->toArray(false); // false for the shallow conversion
       fputcsv($csvh, $data, $d, $e);
   }

   fclose($csvh);

  // do something with the file
}
Sign up to request clarification or add additional context in comments.

3 Comments

Nice solution, please modify fuputcsv to fputcsv.
How well does it work with large datasets ? Isn't it going to fill up the memory ?
With large datasets I tend to batch them.
1

There are existing solutions for data import\export.

Consider using one of this:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.