Skip to content

Commit 4134c34

Browse files
Daniel BerthereauDaniel Berthereau
authored andcommitted
Fixed storage of data type values.
1 parent afc3010 commit 4134c34

1 file changed

Lines changed: 35 additions & 16 deletions

File tree

Module.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,8 @@ protected function copyMarkersToCoordinates(array $ids, array $data): void
717717
*
718718
* @see \NumericDataTypes\Module::saveNumericData()
719719
*
720+
* Unlike NumericDataTypes, the same table is used for multiple types.
721+
*
720722
* @param Event $event
721723
*/
722724
public function saveGeometryData(Event $event): void
@@ -727,13 +729,30 @@ public function saveGeometryData(Event $event): void
727729
return;
728730
}
729731

730-
$entityValues = $entity->getValues();
731-
732732
$services = $this->getServiceLocator();
733733
$entityManager = $services->get('Omeka\EntityManager');
734734
$srid = $services->get('Omeka\Settings')
735735
->get('datatypegeometry_locate_srid', Geography::DEFAULT_SRID);
736736

737+
// All rows in data_type_geometry and data_type_geography are removed,
738+
// so get all ids one time.
739+
$entityValues = $entity->getValues();
740+
741+
$entityDataTypeValues = [
742+
'geography' => [],
743+
'geometry' => [],
744+
];
745+
if ($entity->getId()) {
746+
foreach (array_keys($entityDataTypeValues) as $mainGeo) {
747+
$mainGeoUpper = ucfirst($mainGeo);
748+
$dql = "SELECT dtv FROM \DataTypeGeometry\Entity\DataType$mainGeoUpper dtv WHERE dtv.resource = :resource";
749+
$query = $entityManager->createQuery($dql);
750+
$query->setParameter('resource', $entity);
751+
$entityDataTypeValues[$mainGeo] = $query->getResult();
752+
}
753+
}
754+
755+
/** @var \DataTypeGeometry\DataType\AbstractDataType $dataType */
737756
foreach ($this->getGeometryDataTypes() as $dataTypeName => $dataType) {
738757
// TODO Improve criteria when the types are mixed in a property.
739758
$criteria = Criteria::create()->where(Criteria::expr()->eq('type', $dataTypeName));
@@ -743,22 +762,19 @@ public function saveGeometryData(Event $event): void
743762
continue;
744763
}
745764

746-
$dataTypeClass = $dataType->getEntityClass();
747-
$currentSrid = in_array($dataTypeName, ['geography', 'geography:coordinates']) ? $srid : 0;
748-
749765
// TODO Remove this persist, that is used only when a geometry is updated on the map.
750766
// Persist is required for annotation, since there is no cascade
751767
// persist between annotation and values.
752-
$entityManager->persist($entity);
768+
if ($entity instanceof \Annotate\Entity\Annotation) {
769+
$entityManager->persist($entity);
770+
}
771+
772+
$mainGeo = strtok($dataTypeName, ':');
773+
$currentSrid = $mainGeo === 'geography' ? $srid : 0;
774+
$dataTypeClass = $dataType->getEntityClass();
753775

754776
/** @var \DataTypeGeometry\Entity\DataTypeGeometry[] $existingDataValues */
755-
$existingDataValues = [];
756-
if ($entity->getId()) {
757-
$dql = sprintf('SELECT n FROM %s n WHERE n.resource = :resource', $dataTypeClass);
758-
$query = $entityManager->createQuery($dql);
759-
$query->setParameter('resource', $entity);
760-
$existingDataValues = $query->getResult();
761-
}
777+
$existingDataValues = &$entityDataTypeValues[$mainGeo];
762778

763779
foreach ($matchingValues as $value) {
764780
// Avoid ID churn by reusing data rows.
@@ -787,20 +803,23 @@ public function saveGeometryData(Event $event): void
787803
$dataValue->setValue($geometry);
788804
}
789805

790-
// Remove any data values that weren't reused.
806+
unset($existingDataValues);
807+
}
808+
809+
// Remove any data values that weren't reused.
810+
foreach ($entityDataTypeValues as &$existingDataValues) {
791811
foreach ($existingDataValues as $existingDataValue) {
792812
if ($existingDataValue !== null) {
793813
$entityManager->remove($existingDataValue);
794814
}
795815
}
796816
}
817+
unset($existingDataValues);
797818
}
798819

799820
/**
800821
* Get all data types added by this module.
801822
*
802-
* Note: this is compliant with Omeka 1.2.
803-
*
804823
* @return \Omeka\DataType\AbstractDataType[]
805824
*/
806825
public function getGeometryDataTypes()

0 commit comments

Comments
 (0)