Skip to content

Commit d630b71

Browse files
committed
CleadIndexCommand & refactoring of "getConfigFieldName" method
1 parent 52741aa commit d630b71

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

Command/ClearIndexCommand.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,32 @@ private function deleteByEntityConfig($entityConfig, $id = null)
135135
$schemaName = $entityConfig['schema'];
136136
$updateQuery = $this->updateQueryBuilder->buildUpdateQueryBySchemaName($schemaName);
137137
$schema = $this->config->getSchemaByName($schemaName);
138-
$discriminatorField = $schema->getDiscriminatorConfigField();
139-
$discriminatorValue = $discriminatorField->getValue($entityConfig);
140138

141139
if (empty($id)) {
142-
$updateQuery->addDeleteCriteriaByField(
140+
$discriminatorField = $schema->getDiscriminatorConfigField();
141+
$discriminatorValue = $discriminatorField->getValue($entityConfig);
142+
$updateQuery->addDeleteCriteriaByConfigField(
143143
$discriminatorField->getDocumentFieldName(),
144144
$discriminatorValue
145145
);
146-
$message = sprintf('Removing of %s is completed successfully',
147-
$entityConfig['class']
148-
);
149-
146+
$message = sprintf('Removing of %s is completed successfully', $entityConfig['class']);
150147
} else {
148+
$entityClass = $entityConfig['class'];
149+
$repository = $this->em->getRepository($entityClass);
150+
$entity = $repository->find($id);
151+
152+
if (!$entity) {
153+
throw new EntityNotFoundException(
154+
sprintf('% with %s id is not found', $entityClass, $id)
155+
);
156+
}
157+
151158
$updateQuery->addDeleteCriteriaByUniqueFieldValue(
152-
sprintf('%s-%s', $discriminatorValue, $id)
159+
$schema->getDocumentUniqueField()->getValue($entity, $entityConfig)
153160
);
161+
154162
$message = sprintf('Removing of %s with id %s is completed successfully',
155-
$entityConfig['class'],
163+
$entityClass,
156164
$id
157165
);
158166

Query/Select/AbstractSelectQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ private function prepareField($fieldName, $isConfigField = false)
432432
}
433433

434434
if ($isConfigField) {
435-
$field = $this->schema->getConfigFieldName($fieldName);
435+
$field = $this->schema->getConfigFieldByName($fieldName);
436436
} else {
437437
$field = $this->getField($fieldName);
438438
}

Query/Update/UpdateQuery.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function addConfigField($configFieldName, $value)
119119
throw new UpdateQueryException('Entity is not started. Call "beginEntity" method before add field value');
120120
}
121121

122-
$configEntityField = $this->schema->getConfigFieldName($configFieldName);
122+
$configEntityField = $this->schema->getConfigFieldByName($configFieldName);
123123
$this->addFieldsConditions[$configEntityField->getDocumentFieldName()] = $value;
124124
}
125125

@@ -161,6 +161,16 @@ public function addDeleteCriteriaByField($entityFieldName, $value)
161161
$this->deleteConditions[$field->getDocumentFieldName()] = $value;
162162
}
163163

164+
/**
165+
* @param string $entityFieldName
166+
* @param mixed $value
167+
*/
168+
public function addDeleteCriteriaByConfigField($entityFieldName, $value)
169+
{
170+
$field = $this->schema->getConfigFieldByName($entityFieldName);
171+
$this->deleteConditions[$field->getDocumentFieldName()] = $value;
172+
}
173+
164174

165175
/**
166176
* @param $value

Schema/Schema.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function getClient()
184184
* @param $configFieldName
185185
* @return mixed
186186
*/
187-
public function getConfigFieldName($configFieldName)
187+
public function getConfigFieldByName($configFieldName)
188188
{
189189
if (!array_key_exists($configFieldName, $this->configEntityFields)) {
190190
throw new SchemaConfigException(

Tests/Query/Select/AbstractSelectQueryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testAddConfigFieldOrWhere()
1919
$searchTerm = 'search';
2020

2121
$schema->expects($this->any())
22-
->method('getConfigFieldName')
22+
->method('getConfigFieldByName')
2323
->with($fieldName)
2424
->will($this->returnValue($field));
2525
;

0 commit comments

Comments
 (0)