Convert from Database / Excel / CSV to YAML data fixtures?

13,570

Solution 1

I wrote my own Macro to solve this problem & shared it. You can specify the fields in your model, fill-out the data and the YAML is generated. The best part is that it supports Nested Data as well (based on NestedSet Doctrine Behaviour)

You can download the file from here: http://www.prasadgupte.com/go/converting-csvexcel-data-to-doctrine-yaml-fixtures/

Hope this helps!

Solution 2

A quick google search came up with this: http://code.activestate.com/recipes/546518-simple-conversion-of-excel-files-into-csv-and-yaml/

Requires Python though but that shouldn't be a problem. Looks quite promising and does exactly what you need (keeping in mind that CSV files can be opened with excel like a native excel file and saved as .xls)

Solution 3

If you are already using the conversion macro, then you can add a function that will create a PHP script from the CSV data. So a data row for the object "Pen" like: name type price

Pen Name, Type, Price
"Reyballs Super Point 0.5", "Ball point", 10 
"Palkar Ink Pen", "Ink Pen", 25

would output:

// Object: Pen
$pen1 = new Pen();
$pen1->name = "Reyballs Super Point 0.5";
$pen1->type = "Ball point";
$pen1->price = "10";
$pen1->save();
unset($pen1);
$pen2 = new Pen();
$pen2->name = "Palkar Ink Pen";
$pen2->type = "Ink Pen";
$pen2->price = "25";
$pen2->save();
unset($pen2);

Here is the macro function:

Sub GeneratePHP()

targetSheetRow = 1
fieldNamesRow = 3
sourceSheetDataRow = fieldNamesRow + 1
earlyLoopEnd = False
counter = 0

' do not run without active sheet
If ActiveSheet.Name = "" Then
MsgBox "Please call the macro from a sheet"
End
End If

' identify sheets
Set source = ActiveSheet
' custom output sheet
targetSheetName = source.Cells(1, 12)

If targetSheetName = "" Or targetSheetName = "Output" Then
targetSheetName = "Output"
Else
On Error GoTo RTE
Set Target = Worksheets(targetSheetName)
GoTo RTS
RTE:
'MsgBox "PG" & Err.Description, Title:=Err.Source
targetSheetName = "Output"
End If

RTS:
' clear exsiting data in Target/Output sheet
Set Target = Worksheets(targetSheetName)
Target.Cells.Clear
Target.Cells.Font.Name = "Courier"
' Get no of fields in model (assume level & key always there)
noOfCols = 2
Do While source.Cells(fieldNamesRow, noOfCols + 1) <> "end"
noOfCols = noOfCols + 1
Loop
' If no field other than level & key, error
If noOfCols < 3 Then
MsgBox "No data for the records"
End
End If

' print Model name
Target.Cells(targetSheetRow, 1) = "// Object: " + source.Cells(1, 4)
targetSheetRow = targetSheetRow + 1
objClass = source.Cells(1, 4)

' Loop over data rows in source sheet
Do While source.Cells(sourceSheetDataRow, 1) <> "end"

If source.Cells(sourceSheetDataRow, 1) = "end-loop" Then
Target.Cells(targetSheetRow, 1) = "<?php endfor; ?>"
 targetSheetRow = targetSheetRow + 1
earlyLoopEnd = True
GoTo NextRow
End If

' rows to skip
If source.Cells(sourceSheetDataRow, 2) = "~!~" Or source.Cells(sourceSheetDataRow, 1) = "~!~" Then
GoTo NextRow
End If

' read level
blanks = source.Cells(sourceSheetDataRow, 1)

' print key
counter = counter + 1
varName = "$" + LCase(objClass) + CStr(counter)
varDec = varName + " = new " + objClass + "();"
Target.Cells(targetSheetRow, 1) = varDec
targetSheetRow = targetSheetRow + 1
spaces = spaces + "  "
spaces_count = spaces_count + 2

' print fields when value != ~!~
For clNumber = 3 To noOfCols
 If CStr(source.Cells(sourceSheetDataRow, clNumber)) <> "~!~" And CStr(source.Cells(fieldNamesRow, clNumber)) <> "~!~" Then
  Target.Cells(targetSheetRow, 1) = varName + "->" + source.Cells(fieldNamesRow, clNumber) + " = """ + CStr(source.Cells(sourceSheetDataRow, clNumber)) + """;"
  targetSheetRow = targetSheetRow + 1
 End If
Next clNumber

Target.Cells(targetSheetRow, 1) = varName + "->save();"
  targetSheetRow = targetSheetRow + 1
Target.Cells(targetSheetRow, 1) = "unset(" + varName + ");"
  targetSheetRow = targetSheetRow + 1

NextRow:
' go for next row in source sheet
sourceSheetDataRow = sourceSheetDataRow + 1

Loop

' Success
msg = "Data from sheet """ & source.Name & """ was converted to YAML on """ & targetSheetName & """ sheet" & vbCrLf & vbCrLf & "prasadgupte.com"
MsgBox msg
' Focus on output sheet
Sheets(targetSheetName).Select
Range("A1:A" & (targetSheetRow - 1)).Select
End Sub

Solution 4

You can try Data Transformer (disclaimer - I'm its developer). It converts between CSV, JSON, XML, and YML locally.

It has many conversion settings (with good defaults) so you can tailor the result for your purposes.

You can get it from the Mac App Store or the Microsoft Store.

There's not trial version, but you can ask Apple or Microsoft for a refund if it doesn't work for you!

Share:
13,570
Prasad
Author by

Prasad

Product ninja. UX devotee. Technology geek. Experiential learner. Innovating right products, leading products right. Zycus, L&amp;T, BITS Pilani alumnus. Husband, Son, Foodie, Storyteller, Wantrepreneur. Tweets #prodmgmt #ux #agile #culture #food #travel

Updated on June 04, 2022

Comments

  • Prasad
    Prasad about 2 years

    Was wondering if there is an easy to convert structured files into YAML data fixtures for Doctrine / Symfony.

    I don't see any utility with Doctrine to accept CSV.

    I might just start writing something simple to do this. Is it worthwhile?

  • Prasad
    Prasad almost 14 years
    thanks DrColossos, I had bumped onto this... but have never worked with Python before - don't even know what it would take to run a script like this.. I will try to achieve something simpler.. and post it here... thanks
  • Prasad
    Prasad over 10 years
    UpVotes don't matter personally but it instils confidence in a answer for seekers. 300+ visits/downloads and just 2 upvotes here..