2

I'm trying to insert an entry along with values for a table field. According to the docs, the setFieldValues method takes an array of values in the format handle => value, but it doesn't specify how the value should be formatted for each field type.

Here's my best guess at how it might work for the table field:

$entry = new Entry();
$entry->sectionId = 1;
$entry->typeId = 1;
$entry->title = $title;
$entry->setFieldValues([
  "field_handle" => [
    ["column_1_handle" => "hello", "column_2_handle" => "world"], // Row 1
    ["column_1_handle" => "more", "column_2_handle" => "stuff"] // Row 2
  ]
]);

This inserts the correct number of rows into the table field, but the values are empty, so I'm guessing I'm structuring the array incorrectly.

Any ideas?

1 Answer 1

5

I figured this out. The table field is expecting JSON:

$entry = new Entry();
$entry->sectionId = 1;
$entry->typeId = 1;
$entry->title = $title;
$entry->setFieldValues([
  "field_handle" => '[{"col1":"hello","col2":"world"},{"col1":"more","col2":"stuff"}]'
]);
1
  • Great answer thank you! There is the json_encode([ 'col1' => 'hello', 'col2' => 'world' ]) which may be useful for others :) Commented Nov 7, 2021 at 4:28

Your Answer

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

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