I'm working on a site that has an existing eck entity type and I want to create an instance of that entity. For non-ECK things, I'm using Node::create(), etc. When I run the same code with EckEntity::create(), I get Drupal\Core\Entity\Exception\AmbiguousEntityClassException: Multiple entity types found for Drupal\eck\Entity\EckEntity.

How can I create a specific EckEntity instance programmatically? As an example, there's an entity type example with a bundle of example_day. I haven't been able to nail down any way around this exception.

Thanks!

Comments

rballou created an issue. See original summary.

rballou’s picture

Title: How do you create an EckEntity programmatically. » How do you create an EckEntity programmatically?
legolasbo’s picture

You could try the following:

$entity_manager = \Drupal::entityManager();
$entity_manager->getStorage('example')->create();

Where 'example' is replaced with the entity type.

rballou’s picture

I should have updated this: I did have luck with entity_create(). Using the entity class doesn't work because of the error above. I'm just not sure if there is a way around it.

FWIW, Drupal::entityManager() is deprecated.

joecrespo’s picture


$entity_type_mgr = \Drupal::getContainer()->get('entity_type.manager');
$entity = $entity_type_mgr->getStorage('example')->create(
  array(
    'type' => 'example_day',
  )
);

rballou’s picture

I see what you did there, Joe. :)

The storage class does seem to work. I guess that might be the more "unified" way to get that built with everything else.

legolasbo’s picture

Status: Active » Fixed

I'm marking this issue fixed since it answers the question how to create an ECK entity. I do agree there is a need to refactor the module to better comply with the way other entities are created troughout core, but that is something for another issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

drupalninja99’s picture

Could we add this to the documentation?

legolasbo’s picture

Feel free to submit a patch to add this to eck.api.php

nnevill’s picture

Here is working code:

$entity_type_mgr = \Drupal::getContainer()->get('entity_type.manager');
    $entity = $entity_type_mgr->getStorage('entity_type')->create(
      array(
        'type' => 'entity_bundle',
        'title' => 'dummy title',
      )
    )->save();
man-1982’s picture

Please, tell me. How create ECK (programmatically) with some fields, ex .field_description, field_amount?

paul_leclerc’s picture

Few years after @man-1982, that's how I did it.

http://xxxxx:8020/admin/structure/eck/ENTITY_TYPE/bundles/ENTITY_BUNDLE/...

$eck = [
      'entity_type'                => 'ENTITY_TYPE',
      'type'                       => 'ENTITY_BUNDLE',
      'title'                      => $myTitle,
      'field_userappstatus_app'    => $appId,
      'field_userappstatus_status' => $statusId,
];
$userAppStatus = Drupal\eck\Entity\EckEntity::create($eck);
$userAppStatus->save();

You'll need this patch : https://www.drupal.org/files/issues/2019-05-02/eck-create_method_fixes_m...
Allowing ECK Entity to create an entity when you have several entity types created.

avinashm’s picture

Thank you @paul_leclerc for the reference code.