3

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.

1

Please note this is UNTESTED but written from the actual code files so should play fair...

//load up the grid model for later use
ee()->load->model('grid_model');

//first we create our field using the channel_field_api (your code)
$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);

//now we have field ID we create our data table using the grid model
if ( ee()->grid_model->create_field($field_id, 'grid') == false ) {
    //handle error here
}

//next we add some columns (one in this case) again with the grid model
$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',
    )),
);
$new_col_id = ee()->grid_model->save_col_settings($file_column, FALSE);

Give this a try, be sure to read the comments in Grid_Model.php as you go, and also check the return values and the database as you go through :)

| improve this answer | |
  • Finally got it working! Thanks so much for helping me try and make heads-and-tails of the grid column! – Martin Bean Apr 8 '15 at 13:20
  • Very happy to have helped, Grid was a bit of a mind-melter, but I'd previously deconstructed matrix for custom reasons so this helped understanding somewhat! PS> if you could do me a favour and up-vote the answer as well (thanks for the tick) I'm trying to collect a bit of rep together :) – Blatant Apr 8 '15 at 13:35
1

Have you had a look at the Grid Model that is in the system folder? Specifically /system/expressionengine/models/grid_model.php

This model has methods for creating new Grid Fields (create_field on line 140), add and update columns (save_col_settings on lone 235) and saving data into the grid (save_field_data on line 689). Is this the API you are looking for?

Surfice to say, this Model shows you the code to create grids, columns and data. With a little searching you could probably find the code that calls these model functions for examples on how to use (appologies I don't have a test enviroment to post actual code for you atm) I suspect in the /system/expressionengine/fieldtypes/ft.grid.php is a good place to start for this. Another place is the apply_settings function on line 606 of /system/expressionengine/fieldtypes/libraries/Grid_lib.php

In order to load and use the model, all you need to do in your model is :

//load the Grid Model
ee()->load->model('grid_model');

//create a new Grid field (PSEUDO)
$settings = array(
    'settings_for_grid' => true
);
$new_field = ee()->grid_model->create_field($settings, 'grid');

//create a column on the new grid
$col_id = 1;
$col_settings = array(
    'settings_for_column' => true
);
$col_ids[] = ee()->grid_model->save_col_settings($col_settings, $col_id = 1, 'grid');

Seems straight forward enougth, although determining your settings (for the columns look to like 645 ;) ) might be a pain it should work, after all this is how the admin CP creates the fields!

Let us know how you get on mate!


**Edit - Added Column creation code for comment below

// Add the fieldtype's columns to our data table
ee()->api_channel_fields->setup_handler($column['col_type']);
ee()->api_channel_fields->set_datatype(
    $col_id,
    json_decode($column['col_settings'], TRUE),
    array(),
    TRUE,
    FALSE,
    $this->_get_ft_api_settings($column['field_id'], $content_type)
);

The code above is fromthe Grid_Model's save_col_settings function. After it creates the entry in the grid_columns table, where $col_id is the returned column_id (iterate youd $col_ids[]) it creates the "fieldtype's column" as they call it, which will hold the data value.

That'll be what your missing! Assuming you are indeed missing the columns in the data table.

| improve this answer | |
  • I’ve created my grid field and columns. The issue is the error being triggered on save. – Martin Bean Apr 7 '15 at 17:54
  • When you 'Save Entry' do you mean via the CP? I assume that because you didn't post any code for saving your entry this error is coming via CP. This would indicate the column creation code isn't quite right. I note your code creates an entry in exp_grid_columns but did you create a column in exp_channel_grid_field_457 too? You'll need to do that, the Grid_model would add the column for you but as you're creating manually I would guess that your table exp_channel_grid_field_457 (for Grid this is exp_channel_grid_field_ + $field_id) is missing its columns... – Blatant Apr 8 '15 at 8:54
  • I looked up the column save function in the Grid Model (line 235ish) to reference the exact code that creates the columns for you, and I've edited my post above with example for you mate. – Blatant Apr 8 '15 at 9:01
  • I’m getting this error when activating my extension now: gist.github.com/martinbean/b498bcbf59d912a29303 – Martin Bean Apr 8 '15 at 10:27
  • Hi Martin, you're getting a bit confused between how the modle functions work and how to do it manually with db class (create_field seems to create the data table for you only. No other actions) I'm going to write you a different answer below based on your code in your link. (Plus my example was written wrong) – Blatant Apr 8 '15 at 11:54

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.