I am trying to create an entity programmatically.

My code:

//my entity
use \Drupal\my_module\Entity\my_entity;

$entity = my_entity::create(
          'label' => "token",
          'type' => "bundle",
          'uuid' => $uuid,
          'user_id' => $user->id(),
          'entity_type' => $node->getType(),
          'entity_id' => $node->id(),
          'value' => 10,
          'read' => false,
          'created' => time(),
          'changed' => time()

However, on save, it gives me this sql error. What am I missing?

Drupal\Core\Entity\EntityStorageException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'read, created, changed) VALUES ('bundle', '4a6ca0e3-89f0-46da-8889-c42a7f45ec4a'' at line 1: INSERT INTO {my_table} (type, uuid, user_id, entity_type, entity_id, value, read, created, changed) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => bundle [:db_insert_placeholder_1] => 4a6ca0e3-89f0-46da-8889-c42a7f45ec4a [:db_insert_placeholder_2] => 2 [:db_insert_placeholder_3] => article [:db_insert_placeholder_4] => 14 [:db_insert_placeholder_5] => 10 [:db_insert_placeholder_6] => 0 [:db_insert_placeholder_7] => 1493818295 [:db_insert_placeholder_8] => 1493818295 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 770 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).


https://www.drupal.org/docs/develop/coding-standards/list-of-sql-reserved-words Maybe "read" should have quotes (``) around it in order for this to work. I am uncertain why this is not done automatically.

  • No, that's not the case. read is boolean type. – Adarsh Khatri May 3 '17 at 13:45
  • the word "read", not the value of it. – Ruben Beekman May 3 '17 at 13:46
  • Tried with 'read' => 'false' or "read" => "false" both doesn't work and same error thrown. – Adarsh Khatri May 3 '17 at 13:47
  • "`read`" => false – Ruben Beekman May 3 '17 at 13:48
  • Same error, very strange. – Adarsh Khatri May 3 '17 at 13:50

This is not the exact one but You can check this example.

use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;

// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_REPLACE);

// Create node object with attached file.
$node = Node::create([
  'type'        => 'article',
  'title'       => 'Druplicon test',
  'field_image' => [
    'target_id' => $file->id(),
    'alt' => 'Hello world',
    'title' => 'Goodbye world'

More Here


!Ahhh. What a silly idea to get caught in the middle.

When I checked manually in phpmyadmin it says read is system reserved. Posting this answer as separate so that it will be clear enough for others. @Ruben deserves the point for that link:

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.