Commit 58bbff9d authored by Dries's avatar Dries
Browse files

Issue #1847582 by amateescu: Create a foreign key to the target entity type...

Issue #1847582 by amateescu: Create a foreign key to the target entity type base type in Entity-reference.
parent 0311348b
......@@ -28,22 +28,21 @@ function entity_reference_field_schema($field) {
'indexes' => array(
'target_id' => array('target_id'),
),
'foreign keys' => array(),
);
// Create a foreign key to the target entity type base type.
// @todo It's still not safe to call entity_get_info() in here.
// see http://drupal.org/node/1847582
// $entity_type = $field['settings']['target_type'];
// $entity_info = entity_get_info($entity_type);
//
// $base_table = $entity_info['base_table'];
// $id_column = $entity_info['entity_keys']['id'];
//
// $schema['foreign keys'][$base_table] = array(
// 'table' => $base_table,
// 'columns' => array('target_id' => $id_column),
// );
$entity_manager = Drupal::service('plugin.manager.entity');
if (is_subclass_of($entity_manager->getControllerClass($field['settings']['target_type'], 'storage'), 'Drupal\Core\Entity\DatabaseStorageController')) {
$entity_info = $entity_manager->getDefinition($field['settings']['target_type']);
$base_table = $entity_info['base_table'];
$id_column = $entity_info['entity_keys']['id'];
$schema['foreign keys'][$base_table] = array(
'table' => $base_table,
'columns' => array('target_id' => $id_column),
);
}
return $schema;
}
......@@ -98,4 +98,32 @@ public function testEntityReferenceItem() {
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id());
$this->assertEqual($entity->field_test_taxonomy->entity->name->value, $term2->name->value);
}
/**
* Tests foreign key support.
*/
public function testEntityReferenceFieldSchema() {
$field = field_info_field('field_test_taxonomy');
$foreign_key_column_name = 'target_id';
// Grab the SQL schema and verify that the 'foreign keys' are present.
$schemas = _field_sql_storage_schema($field);
$schema = $schemas[_field_sql_storage_tablename($field)];
$this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema.');
$foreign_key = reset($schema['foreign keys']);
$foreign_key_column = _field_sql_storage_columnname($field['field_name'], $foreign_key_column_name);
$this->assertEqual($foreign_key['table'], 'taxonomy_term_data', 'Foreign key table name preserved in the schema.');
$this->assertEqual($foreign_key['columns'][$foreign_key_column], 'tid', 'Foreign key column name preserved in the schema.');
// Create a field that references a config entity type and check that no
// foreign key is present.
$field_name = 'field_test_vocabulary';
entity_reference_create_instance('entity_test', 'entity_test', $field_name, 'Test vocabulary reference', 'taxonomy_vocabulary');
$field = field_info_field($field_name);
$schemas = _field_sql_storage_schema($field);
$schema = $schemas[_field_sql_storage_tablename($field)];
$this->assertFalse(isset($schema['foreign keys']), 'There is no foreign key in the schema.');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment