I’m creating an ExpressingEngine extension that upon activation, automatically creates a channel with fields. One of the fields the extension creates is a grid field. I create the field like this:
$floor_plans_field = array(
'field_name' => 'floor_plans',
'field_label' => 'Floor Plans',
'field_type' => 'grid',
'field_order' => 30,
'field_instructions' => 'The floor plans for the property',
'field_required' => 'n',
'field_search' => 'n',
'field_is_hidden' => 'n',
);
$field = array_merge($floor_plans_field, array(
'group_id' => $group_id,
'site_id' => 1,
));
$field_id = $this->EE->api_channel_fields->update_field($field);
$file_column = array(
'field_id' => $field_id,
'content_type' => 'channel',
'col_order' => 1,
'col_type' => 'file',
'col_label' => 'File',
'col_name' => 'file',
'col_instructions' => 'A floor plan.',
'col_required' => 'y',
'col_search' => 'n',
'col_settings' => json_encode(array(
'field_content_type' => 'all',
'show_existing' => 'y',
'field_required' => 'y',
)),
);
$query = $this->EE->db->insert('grid_columns', $file_column);
It creates the grid field for floor plans, and a single column for file (so the user can add multiple floor plans to the entry).
However, when I save the entry I get the following error:
A Database Error Occurred Error Number: 1054
Unknown column 'col_id_5' in 'field list'
INSERT INTO `exp_channel_grid_field_457` (`col_id_5`, `entry_id`, `row_order`) VALUES ('{filedir_1}banner1.jpg',62,0)
Filename: models/grid_model.php
Line Number: 767
It seems as though the ID of the grid column isn’t getting replaced.
Why is this? What am I doing wrong? Any help here would be appreciated as documentation on the grid field is… sparse.