I'm trying to import data from another database into a 'table' fieldtype via a plugin, and have a couple questions.

When setting the content on the EntryModel, are there any methods to assist in setting the rows and/or columns, or do I need to just format the data as a string in the expected format?

        'tableFieldHandle' => '[{"col1":"data","col2":"data"}]'

Secondly, what's the recommended method to prepare strings for a table field (i.e. so as to escape or encode double and single quotes)?


The thing to keep in mind here is that a table field contains a simple JSON string. So you can (and should) use PHP's native json_encode function to prepare your data...

// Your data will be an array of arrays:
$tableData = array(
        "col1" => "data",
        "col2" => "data",

// Then simply encode it when you set your field value:
    'tableFieldHandle' => json_encode($tableData)

This will also solve any string escaping concerns for you!

  • That makes total sense. Thank you Lindsey! – Douglas McDonald Feb 1 '15 at 4:08
  • Just incase anyone else stumbles on to this - You actually have to use "col1" etc and not the handle of your column... – mylesthe.dev Mar 10 '20 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.