/** * Delete an entity by its primary key / identifier. * * @param array|object $identifiers * @return bool * @throws \Exception */ public function delete($identifiers) { if (is_object($identifiers)) { $identifiers = $this->_hydrator->extract($identifiers); } if (is_string($identifiers)) { $field = $this->_class->getIdentifierFieldNames(); $identifiers = [$field[0] => $identifiers]; } $classIdentifiers = $this->_class->getIdentifier(); $identifiers = array_intersect_key($identifiers, $classIdentifiers); $result = $this->_em->getConnexionDriver()->deleteItem($identifiers, $this->_class); if (!$result) { throw new \Exception('Something wrong.'); } return $result ? true : false; }
/** * @param ClassMetadata $classMetadata * @return null */ public function createTable(ClassMetadata $classMetadata) { $attributesList = []; $model = ['AttributeDefinitions' => [], 'KeySchema' => [], 'ProvisionedThroughput' => ['ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1]]; $indexes = $classMetadata->getIndexes(); // We prevent that index from primary keys are not included unset($indexes[$classMetadata->buildHash($classMetadata->getIdentifierFieldNames())]); if (count($indexes) > 0) { $model['GlobalSecondaryIndexes'] = []; foreach ($indexes as $index) { $keys = []; foreach ($index['fields'] as $key => $type) { $keys[] = ['AttributeName' => $key, 'KeyType' => $type]; $attributeType = $this->mapKeyTypeField($classMetadata->getTypeOfField($key)); if (is_null($attributeType)) { throw new IncompatibleTypeException('We cannot create a GSI with key `' . $key . '` and type `' . $classMetadata->getTypeOfField($key) . '`.'); } $attributesList[$key] = ['AttributeName' => $key, 'AttributeType' => $attributeType]; } $model['GlobalSecondaryIndexes'][] = ['IndexName' => $index['name'], 'KeySchema' => $keys, 'Projection' => ['ProjectionType' => 'ALL'], 'ProvisionedThroughput' => ['ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1]]; } } foreach ($classMetadata->getIdentifier() as $key => $field) { $attributesList[$key] = ['AttributeName' => $field['name'], 'AttributeType' => $this->mapTypeField($field['type'])]; $model['KeySchema'][] = ['AttributeName' => $field['name'], 'KeyType' => $field['key']]; } $model['AttributeDefinitions'] = array_values($attributesList); $model['TableName'] = $this->getTableName($classMetadata); return $this->commit('createTable', $model) ? true : false; }