Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChannelField model parameter Field Order is required #3069

Closed
blairliikala opened this issue Mar 2, 2023 · 0 comments · Fixed by #3081
Closed

ChannelField model parameter Field Order is required #3069

blairliikala opened this issue Mar 2, 2023 · 0 comments · Fixed by #3081
Labels
Bug: Accepted Bug has been confirmed, is reproducible, and ready to work on. needs documentation

Comments

@blairliikala
Copy link

When creating a field programmatically using the ChannelField model, the parameter field_order appears to be required. When creating with this code:

$new_field = ee('Model')->make('ChannelField');
$new_field->set(
  "field_type"         => "my_custom_field_type" ,
  "field_label"        => "my_custom_field_label",
  "field_name"         => "my_custom_field_name",
  "field_instructions" => "text",
  "field_search"       => "y",
  "field_fmt"          => "none",
  "field_show_fmt"     => "n",
  "field_list_items"   => '',
);
$new_field->save();

The following error occurs:

SQLSTATE[HY000]: General error: 1364 Field 'field_order' doesn't have a default value:
INSERT INTO `exp_channel_fields` (`field_type`, `site_id`, `field_name`, `field_label`, `field_instructions`, `field_list_items`, `field_search`, `field_fmt`, `field_show_fmt`, `field_settings`) VALUES ('mux_asset', 1, 'mux_asset_field', 'Mux Asset', 'Video On-demand field', '', 'y', 'none', 'n', 'YToxMTp7.....etc')

ee/legacy/database/drivers/mysqli/mysqli_connection.php:114

#0 ee/legacy/database/drivers/mysqli/mysqli_driver.php(112): CI_DB_mysqli_connection->query('INSERT INTO `ex...')
#1 ee/legacy/database/DB_driver.php(260): CI_DB_mysqli_driver->_execute('INSERT INTO `ex...')
#2 ee/legacy/database/DB_driver.php(175): CI_DB_driver->simple_query('INSERT INTO `ex...')
#3 ee/legacy/database/DB_active_rec.php(1297): CI_DB_driver->query('INSERT INTO `ex...')
#4 ee/ExpressionEngine/Service/Model/Query/Insert.php(70): CI_DB_active_record->insert('channel_fields')
#5 ee/ExpressionEngine/Service/Model/Query/Update.php(93): ExpressionEngine\Service\Model\Query\Insert->actOnGateway(Object(ExpressionEngine\Service\Model\SyntheticGateway), Object(ExpressionEngine\Model\Channel\ChannelField))
#6 ee/ExpressionEngine/Service/Model/Query/Insert.php(38): ExpressionEngine\Service\Model\Query\Update->doWork(Object(ExpressionEngine\Model\Channel\ChannelField))
#7 ee/ExpressionEngine/Service/Model/Query/Insert.php(27): ExpressionEngine\Service\Model\Query\Insert->doWork(Object(ExpressionEngine\Model\Channel\ChannelField))
#8 ee/ExpressionEngine/Service/Model/DataStore.php(275): ExpressionEngine\Service\Model\Query\Insert->run()
#9 ee/ExpressionEngine/Service/Model/DataStore.php(230): ExpressionEngine\Service\Model\DataStore->runQuery('Insert', Object(ExpressionEngine\Service\Model\Query\Builder))
#10 ee/ExpressionEngine/Service/Model/Query/Builder.php(80): ExpressionEngine\Service\Model\DataStore->insertQuery(Object(ExpressionEngine\Service\Model\Query\Builder))
#11 ee/ExpressionEngine/Service/Model/Model.php(362): ExpressionEngine\Service\Model\Query\Builder->insert()
#12 ee/ExpressionEngine/Model/Content/FieldModel.php(141): ExpressionEngine\Service\Model\Model->save()
#13 user/addons/mux/upd.mux.php(93): ExpressionEngine\Model\Content\FieldModel->save()
#14 ee/legacy/libraries/addons/Addons_installer.php(71): Mux_upd->install()
#15 ee/legacy/libraries/addons/Addons_installer.php(325): Addons_installer->install_module('mux')
#16 ee/legacy/libraries/addons/Addons_installer.php(37): Addons_installer->_update_addon('mux', 'module', 'install', false)
#17 ee/ExpressionEngine/Controller/Addons/Addons.php(1437): Addons_installer->install('mux', 'module', false)
#18 ee/ExpressionEngine/Controller/Addons/Addons.php(614): ExpressionEngine\Controller\Addons\Addons->installModule('mux')
#19 [internal function]: ExpressionEngine\Controller\Addons\Addons->install(Array)
#20 ee/ExpressionEngine/Core/Core.php(268): call_user_func_array(Array, Array)
#21 ee/ExpressionEngine/Core/Core.php(124): ExpressionEngine\Core\Core->runController(Array)
#22 ee/ExpressionEngine/Boot/boot.php(184): ExpressionEngine\Core\Core->run(Object(ExpressionEngine\Core\Request))
#23 index.php(143): require_once('...')
#23 index.php(143): require_once('...')

Since the error references field_order not having a default value, if that is added then there is no error.

$new_field = ee('Model')->make('ChannelField');
$new_field->set(
  "field_type"         => "my_custom_field_type" ,
  "field_label"        => "my_custom_field_label",
  "field_name"         => "my_custom_field_name",
  "field_instructions" => "text",
  "field_search"       => "y",
  "field_fmt"          => "none",
  "field_show_fmt"     => "n",
  "field_list_items"   => '',
  "field_order"        => 0, // ADDED
);
$new_field->save();

The model validation doesn't list that this parameter is required, but maybe it should be? The same is true for field_list_items.

Ideally we should document what the minimum number of parameters are required to create a field this way, and it should be reflected in the model validation, but it seems that they are not quite in sync.

Development oriented environment:
EE 7.2.10
PHP 8.2.0
MySQL 5.7.39
Nginx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug: Accepted Bug has been confirmed, is reproducible, and ready to work on. needs documentation
Projects
None yet
2 participants