Skip to content

Commit

Permalink
delete relations on field deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Jan 25, 2019
1 parent 7da3ce3 commit a3d7dc1
Showing 1 changed file with 141 additions and 2 deletions.
143 changes: 141 additions & 2 deletions src/core/Directus/Services/TablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class TablesService extends AbstractService
*/
protected $collectionsTableGateway;

/**
* @var RelationalTableGateway
*/
protected $relationsTableGateway;

public function __construct(Container $container)
{
parent::__construct($container);
Expand Down Expand Up @@ -728,6 +733,10 @@ public function dropColumn($collectionName, $fieldName)
}
}

if ($columnObject->hasRelationship()) {
$this->removeColumnRelationship($columnObject);
}

if ($columnObject->isManaged()) {
$this->removeColumnInfo($collectionName, $fieldName);
}
Expand Down Expand Up @@ -845,6 +854,124 @@ public function removeColumnInfo($collectionName, $fieldName)
]);
}

/**
* Removes the relationship of a given field
*
* @param Field $field
*
* @return bool|int
*/
public function removeColumnRelationship(Field $field)
{
if (!$field->hasRelationship()) {
return false;
}

if ($this->shouldRemoveRelationshipRecord($field)) {
$result = $this->removeRelationshipRecord($field);
} else {
$result = $this->removeRelationshipFromRecord($field);
}

return $result;
}

/**
* Checks whether or not the relationship record should be removed
*
* @param Field $field
*
* @return bool
*/
protected function shouldRemoveRelationshipRecord(Field $field)
{
$relationship = $field->getRelationship();
$isOne = ($field->getName() === $relationship->getFieldOne());

return ($isOne && !$relationship->getFieldMany())
|| (!$isOne && !$relationship->getFieldOne());
}

/**
* Removes the relationship record of a given field
*
* @param Field $field
*
* @return int
*/
protected function removeRelationshipRecord(Field $field)
{
$tableGateway = $this->getRelationsTableGateway();
$conditions = $this->getRemoveRelationshipConditions($field);

return $tableGateway->delete($conditions['values']);
}

/**
* Removes the relationship data of a given field
*
* @param Field $field
*
* @return int
*/
protected function removeRelationshipFromRecord(Field $field)
{
$tableGateway = $this->getRelationsTableGateway();
$conditions = $this->getRemoveRelationshipConditions($field);

$data = [
$conditions['field'] => null
];

return $tableGateway->update($data, $conditions['values']);
}

/**
* Returns the conditions values to remove a given field relationship
*
* @param Field $field
*
* @return array
*/
protected function getRemoveRelationshipConditions(Field $field)
{
$fieldName = $field->getName();
$collectionName = $field->getCollectionName();
$fieldAttr = 'field_';
$collectionAttr = 'collection_';

$suffix = $this->getRelationshipAttributeSuffix($field);
$collectionAttr .= $suffix;
$fieldAttr .= $suffix;

return [
'field' => $fieldAttr,
'collection' => $collectionAttr,
'values' => [
$collectionAttr => $collectionName,
$fieldAttr => $fieldName,
],
];
}

/**
* Returns the relationship attribute suffix
*
* @param Field $field
*
* @return string
*/
protected function getRelationshipAttributeSuffix(Field $field)
{
if ($field->getName() === $field->getRelationship()->getFieldOne()) {
$suffix = 'one';
} else {
$suffix = 'many';
}

return $suffix;
}

/**
* @param $collectionName
* @param $fieldName
Expand Down Expand Up @@ -1509,7 +1636,7 @@ protected function mergeSchemaCollection($collectionName, array $collectionData)
protected function getFieldsTableGateway()
{
if (!$this->fieldsTableGateway) {
$this->fieldsTableGateway = $this->createTableGateway('directus_fields');
$this->fieldsTableGateway = $this->createTableGateway(SchemaManager::COLLECTION_FIELDS);
}

return $this->fieldsTableGateway;
Expand All @@ -1521,12 +1648,24 @@ protected function getFieldsTableGateway()
protected function getCollectionsTableGateway()
{
if (!$this->collectionsTableGateway) {
$this->collectionsTableGateway = $this->createTableGateway('directus_collections');
$this->collectionsTableGateway = $this->createTableGateway(SchemaManager::COLLECTION_COLLECTIONS);
}

return $this->collectionsTableGateway;
}

/**
* @return RelationalTableGateway
*/
protected function getRelationsTableGateway()
{
if (!$this->relationsTableGateway) {
$this->relationsTableGateway = $this->createTableGateway(SchemaManager::COLLECTION_RELATIONS);
}

return $this->relationsTableGateway;
}

protected function getAllFieldsParams(array $params)
{
$newParams = [];
Expand Down

0 comments on commit a3d7dc1

Please sign in to comment.