Answers.Body: <p>The thing to keep in mind here is that a table field contains a simple <strong>JSON string</strong>. So you can (and should) use PHP's native <a href="http://php.net/manual/en/function.json-encode.php"><code>json_encode</code></a> function to prepare your data...</p>
  
  <pre class="lang-php prettyprint-override"><code>// Your data will be an array of arrays:
  $tableData = array(
      array(
          "col1" =&gt; "data",
          "col2" =&gt; "data",
      )
  );
  
  // Then simply encode it when you set your field value:
  $entry-&gt;getContent()-&gt;setAttributes(array(
      'tableFieldHandle' =&gt; json_encode($tableData)
  ));
  </code></pre>
  
  <p>This will also solve any string escaping concerns for you!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-10T23:52:10.310Z
Answers.Text: Just incase anyone else stumbles on to this - You actually have to use "col1" etc and not the handle of your column...
Answers.UserDisplayName: mylesthe.dev
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-01T04:08:11.783Z
Answers.Text: That makes total sense. Thank you Lindsey!
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-01T02:31:58.130Z
Answers.LastActivityDate: 2015-02-01T02:44:48.213Z
Answers.LastEditDate: 2015-02-01T02:44:48.213Z
Answers.UserDisplayName: Lindsey D
Body: <p>I'm trying to import data from another database into a 'table' fieldtype via a plugin, and have a couple questions.</p>
  
  <p>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?</p>
  
  <pre><code>    $entry-&gt;getContent()-&gt;setAttributes(array(
          'tableFieldHandle' =&gt; '[{"col1":"data","col2":"data"}]'
      ));
  </code></pre>
  
  <p>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)?</p>
  
Tags: <plugin-development><import><table-field>
Title: Inserting data into a table fieldtype via plugin?
ThreadText: # Inserting data into a table fieldtype via plugin?
  
  (Asked by: username_0 on 2015-02-01)
  
  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?
  
  ```
      $entry->getContent()->setAttributes(array(
          '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)?
  
  **Tags:** plugin-development, import, table-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-02-01. Score: 7 | Accepted answer
  
  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`](http://php.net/manual/en/function.json-encode.php) function to prepare your data...
  
  ```php
  // Your data will be an array of arrays:
  $tableData = array(
      array(
          "col1" => "data",
          "col2" => "data",
      )
  );
  
  // Then simply encode it when you set your field value:
  $entry->getContent()->setAttributes(array(
      'tableFieldHandle' => json_encode($tableData)
  ));
  
  ```
  
  This will also solve any string escaping concerns for you!
  
  > **Comments:**
  > **username_0**: That makes total sense. Thank you username_1!  
  > **username_2**: Just incase anyone else stumbles on to this - You actually have to use "col1" etc and not the handle of your column...  
  
  ---
  
  