Creating new node entity during presave of another node entity causes SQL Exception sometimes

We have a Donation Record Content type and a Donor content type for a Blood Bank management drupal application. A donor can have any number of donation record.

The system does not explicitly require creating a donor record prior to creating a donation record. It creates a donor record during presave of donation record if the donor reference is not provided in the URL. The system gets the reference from the created donor during presave and updates it in the donation record. Hence we now have a donor with a donation record while the user is saving a donation record.

The problem is that new donor record does not get created all the time. We get SQL Exception: SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT savepoint_1 does not exist.

Is there any other way to create the donor so that this problem does not occur or am I doing something wrong.

Drupal 7.61 MySQL 5.7.24 OS: Ubuntu 16.04.5 LTS

Trace: 0 /srv/www/ PDOStatement->execute(Array) 1/srv/www/ DatabaseStatementBase->execute(Array, Array) 2/srv/www/ DatabaseConnection->query(‘ROLLBACK TO SAV…’) 3/srv/www/ DatabaseConnection->rollback(‘savepoint_1’) 4 /srv/www/ DatabaseTransaction->rollback() 5/srv/www/ node_save(Object(stdClass)) 6/srv/www/…(885): entity_save(‘node’, Object(stdClass)) 7 /srv/www/…(114): EntityDrupalWrapper->save() 8/srv/www/…(226): DonorEntity::createNewDonorFromDonationRecord(Object(EntityDrupalWrapper))

Custom Entity View Date Picker

I have a custom entity with a date field in it. I have spent a lot of time reviewing several posts related to this issue:

I have not been able to resolve the issue in my project. I have determined that I needed to add some code in my file such as below

function timeoff_views_data_alter(array &$  data) {   $  data['time_off_day']['timeoffdate']['filter']['id'] = 'datetime'; } 

I have not been able to get the datepicker to work, nor can I get the debugger to stop in the routine. I have better-exposed filters and data popup installed and the date pickers are working on all the other date fields in the site. My custom entity has a created date and then a timeoffdate, date picker in the view works on the created date and not the timeoffdate. But in the add/edit they are both working fine.

I also want to note that the module is timeoff wiht multiple custom entities. The entity that I am specifically working on is time_off_day.

Thanks for any help that you can provide.

Custom entites: always load additional data from referenced (child) entity

I created some custom entity types, let’s call them “parent_entity_type” and “historic_data_entity_type”. The parent entity type is referencing to historic_data_entity_type indicating which data is currently up to date and historic_data_entity_type has a field referencing to the parent for a full historic overview.

Know I want to have a parent entity always be fully loaded with data from referenced historic entity as soon as possible during the entity loading process. Therefore I would prefer to do it in my custom entity class or somewhere at this level instead of using hook_entity_load or similar hooks which are called later in the process.

Is there somebody who can give me a hint how to achive that? Looking at Entity::load() I feel that I might need to setup some sort of custom EntityStorage?!

Referencing entity inside aggregate by local ID

How do you properly reference an entity inside an aggregate? For example, by the index of a list (doesn’t seem smart if you re-order the list); a GUID (thought it was supposed to be local?); or some other incrementing counter?

For example:

class Cart {   public IReadOnlyList<Item> Items => _items.ToList();    public AddItem(Item item)   {   }    public RemoveItem(/* Item or ItemId or index (int)? */) <- Here   {     _items.Remove(xxxxx); // <- And here   }    private readonly List<Item> _items = new List<Item>(); // Maybe need different data structure }  class Item {   // Local identity here?     public int Quantity { get; private set; }    public Item(Product product)   {     // use product properties needed   }    // other item properties } 

It seems wrong for a client to have to return an List<Item> from the Cart to then use the index to remove the Item. Additionally, if I instantiate the Item outside the class prior to adding it, how do I impose a local Id (I would have a GUID if instantiating outside)?

TL;DR how to deal with local identities of entities (i.e. how local is “local”)?

Converting an entity with existing contents to revisionable throws exception

I’m trying to convert a core entity (menu_link_content) to revisionable with existing data. I’m trying to figure out how to accomplish this. I saw this go to guide here –

I have a D8.6 site and I’m trying to make the menu_link_content revisionable. On top of that my site has existing content for custom menus. And I cannot just delete these contents. So these has to be ideally migrated to the new revision tables thats created. When I followed the step by step approach in that guide ‘Section 2’ I get the exception –

“Drupal\Core\Entity\EntityStorageException: The SQL storage cannot change the schema for an existing entity type (menu_link_content) with data. in Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema->onEntityTypeUpdate() (line 347 of /var/www/myproj/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php).”

When I delete all the custom menu links content it works.

Please confirm if my understanding is correct in the Section 2 –

“If you are updating the code for an existing site and your entity type already contains some data, you will also need to provide an upgrade path in addition to the changes explained in part 1. The upgrade path is composed of two things “

  • This means before performing the section 2 steps we have to make sure the Section 1 is done. This includes changing the Entity file annotations and setting fields to revisionable etc.

After this step I added the new revision fields like logs, created, time etc to install file and then created a post_update file and specified the fields which revision is set to true in that array.

Any suggestions or examples are welcome. Thanks!