1

I'm trying to find out how to create relationships between tables that are created in the same modules. I create my entity with the console and I declare several tables. How can I declare shared keys in the baseFieldDefinitions of my entity and tables declared in my address_schema()

My way (is it the right method) :

my_module / address.install> address_schema ()

    'foreign keys' => [
      'lin_id' => [
        'table' => 'coo_address',
        'columns' => [
          'lin_id' => 'lin_id',
        ]
      ]
    ]

my_module / Entity> baseFieldDefinitions

$ fields ['lin_id'] = BaseFieldDefinition :: create ('integer')
      -> setLabel (t ( 'Address'))
      -> setDescription (t ('Une ligne pour l'adresse de réference.'))
      -> setSetting ('unsigned', TRUE)
      -> setDisplayOptions ('view', array (
        'label' => 'hidden',
        'weight' => 2,
      ))
      -> setDisplayOptions ('form', array (
        'type' => 'number',
        'weight' => 2,
      ))
      -> setDisplayConfigurable ('form', TRUE)
      -> setDisplayConfigurable ('view', TRUE);

I wish I had something like, but there is error. coo_address_lin being a table create in address_schema ()

$fields['lin_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Adresse'))
      ->setDescription(t('The refence of the line for the address reference.'))
      ->setSetting('target_type', 'coo_address_lin')
      ->setDefaultValue(0)
      ->setTranslatable(TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'weight' => -4,
      ])
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'weight' => -4,
        'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => '60',
          'autocomplete_type' => 'tags',
          'placeholder' => '',
        ],
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

Browse other questions tagged or ask your own question.