4

I'm trying to create a custom table in my module but I keep getting this error:

Warning: array_key_exists() expects parameter 2 to be array, string given in 

I've been able to step through the code and it seems to break when this is executed:

$newTable = $setup->getConnection()->newTable($installer->getTable('orders'))->addColumn(
    'id', Table::TYPE_INTEGER, null, [
    'identity' => true,
    'unsigned' => true,
    'nullable' => false,
    'primary' => true
], ' Id'

)->addColumn(
    'orderid', Table::TYPE_TEXT,
    255, [
    'nullable' => false
], 'sku'

)->addColumn(
    'last_updated_date', Table::TYPE_DATETIME, null, [
    'nullable' => false
], 'last_updated_date'
)->addColumn(
    'total_price', Table::TYPE_INTEGER, null, [
    'nullable' => false
], 'price'
)->addColumn(
    'order_state', Table::TYPE_TEXT, 255, [
    'nullable' => false
], 'order_state'
)->addColumn(
    'fields', Table::TYPE_TEXT, 2048, [
    'nullable' => false
], 'fields'
)->addColumn(
    'order', Table::TYPE_TEXT, 255, [
    'nullable' => false
], 'o_id'
)->addColumn(
    'comment', Table::TYPE_TEXT, 255, [
    'nullable' => false
], 'comment'
)->addColumn(
    'read', Table::TYPE_SMALLINT, 1, [
    'nullable' => false,
    'default' => 0
], 'read'
)->addColumn(
    'in_stock', Table::TYPE_SMALLINT, null, [
    'nullable' => false,
    'default' => 0
], 'in_stock'

)->addColumn(
    'product_title', Table::TYPE_TEXT, 255, [
    'nullable', false,
], 'title'

)->addColumn(
    'qty', Table::TYPE_NUMERIC, null, [

], 'qty'

);

Can anyone tell what I'm doing wrong here???

This is where the exception occurs

  if (array_key_exists('default', $options)) {
        $default = $options['default'];
    }

vendor/magento/framework/DB/Ddl/Table.php on line 352 
5
  • Use proper data type and try it again. for example, \Magento\Framework\DB\Ddl\Table::TYPE_TEXT.. Because Error shows problem in second argument. Commented Mar 14, 2017 at 17:53
  • The error denotes that you are using a string where it should be an array. If you debug the error message, you can resolve the issue easily. Commented Mar 14, 2017 at 17:54
  • I've been debugging all day, I can see the but setting a break point on the line of failure hasn't retuned anything valuable unless I am missing something see my edit ---- Commented Mar 14, 2017 at 20:20
  • Did you tried my solution? Commented Mar 14, 2017 at 20:24
  • The script is already using the class you suggested... Commented Mar 14, 2017 at 20:45

1 Answer 1

5

Take a look: vendor/magento/framework/DB/Ddl/Table.php::addColumn(), we can see the arguments of addColumn method:

 public function addColumn($name, $type, $size = null, $options = [], $comment = null)
  • string $name the column name - 1st argument.
  • string $type the column data type - 2nd argument.
  • string | int | array $size the column length - 3rd argument.
  • array $options array of additional options - 4th argument.
  • string $comment column description - 5th argument.

In your case, you should check each column again carefully. I saw you have a wrong column product_title:

->addColumn(
    'product_title', Table::TYPE_TEXT, 255, [
    'nullable', false,
], 'title'

The 4th argument was wrong. It should be:

->addColumn(
        'product_title',
        Table::TYPE_TEXT,
        255,
        ['nullable'=> false],
        'title'

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.