diff --git a/src/MySQL.php b/src/MySQL.php index 8c5820e..c2fb184 100644 --- a/src/MySQL.php +++ b/src/MySQL.php @@ -665,7 +665,7 @@ protected function convertDataForExport(array $data, \obo\Carriers\EntityInforma * @return array */ protected function convertDataForImport(array $data, \obo\Carriers\EntityInformationCarrier $entityInformation) { - $convertedData = []; + $convertedDataTemporary = []; $information = $this->informationForEntity($entityInformation); foreach ($data as $propertyName => $propertyValue) { @@ -676,7 +676,22 @@ protected function convertDataForImport(array $data, \obo\Carriers\EntityInforma $entityInformationForPropertyRepositoryName = &$storageInformation["repositories"][$propertyRepositoryName]; if ($entityInformationForPropertyRepositoryName["columns"][$entityInformationForPropertyRepositoryName["toColumnName"][$propertyName]]["autoIncrement"]) continue; - $convertedData[$propertyStorageName][$propertyRepositoryName][$entityInformationForPropertyRepositoryName["toColumnName"][$propertyName]] = ($entityInformationForPropertyRepositoryName["columns"][$entityInformationForPropertyRepositoryName["toColumnName"][$propertyName]]["importFilter"] === null OR $propertyValue === null) ? $propertyValue : $this->dataConverter->{$entityInformationForPropertyRepositoryName["columns"][$entityInformationForPropertyRepositoryName["toColumnName"][$propertyName]]["importFilter"]}($propertyValue, $propertyInformation); + $convertedDataTemporary[$propertyStorageName][$propertyRepositoryName][$entityInformationForPropertyRepositoryName["toColumnName"][$propertyName]] = ($entityInformationForPropertyRepositoryName["columns"][$entityInformationForPropertyRepositoryName["toColumnName"][$propertyName]]["importFilter"] === null OR $propertyValue === null) ? $propertyValue : $this->dataConverter->{$entityInformationForPropertyRepositoryName["columns"][$entityInformationForPropertyRepositoryName["toColumnName"][$propertyName]]["importFilter"]}($propertyValue, $propertyInformation); + } + + $convertedData = []; + $primaryPropertyRepositoryName = $entityInformation->informationForPropertyWithName($entityInformation->primaryPropertyName)->repositoryName ?: $entityInformation->repositoryName; + $primaryPropertyStorageName = $this->getStorageNameForProperty($entityInformation->informationForPropertyWithName($entityInformation->primaryPropertyName)); + + $convertedData[$primaryPropertyStorageName] = [$primaryPropertyRepositoryName => $convertedDataTemporary[$primaryPropertyStorageName][$primaryPropertyRepositoryName]]; + unset($convertedDataTemporary[$primaryPropertyStorageName][$primaryPropertyRepositoryName]); + + foreach ($convertedDataTemporary as $storageName => $storageData) { + if (!isset($convertedData[$storageName])) $convertedData[$storageName] = []; + + foreach ($storageData as $repositoryName => $data) { + $convertedData[$storageName][$repositoryName] = $data; + } } return $convertedData; diff --git a/tests/MySQLTest.phpt b/tests/MySQLTest.phpt index 99401a0..1fce5ac 100644 --- a/tests/MySQLTest.phpt +++ b/tests/MySQLTest.phpt @@ -32,6 +32,12 @@ class MySQLTest extends \Tester\TestCase { "fax" => "+420987654321", ]; + private static $businessContactData = [ + "email" => "business@mail.com", + "phone" => "+420159753456", + "companyName" => " Work s.r.o", + ]; + private static $addressData = [ "owner" => 1, "ownerEntity" => "PersonalContact", @@ -127,6 +133,11 @@ class MySQLTest extends \Tester\TestCase { */ const CONTACTS_REPOSITORY = "obo-test.Contacts"; + /** + * @var string + */ + const BUSINESS_CONTACTS_REPOSITORY = "obo-test.BusinessContacts"; + /** * @var string */ @@ -210,6 +221,17 @@ class MySQLTest extends \Tester\TestCase { return $entity; } + /** + * @param bool $save + * @return \obo\DataStorage\Tests\Assets\Entities\Contact + */ + protected function createBusinessContactEntity($save = true) { + $entity = Assets\Entities\Contact\BusinessManager::entityFromArray(static::$businessContactData); + if ($save) $entity->save(); + + return $entity; + } + /** * @param bool $save * @return \obo\DataStorage\Tests\Assets\Entities\Address @@ -416,6 +438,25 @@ class MySQLTest extends \Tester\TestCase { ); } + public function testInsertExtendedEntity() { + $entity = $this->createBusinessContactEntity(false); + $this->storage->insertEntity($entity); + $entity->save(); + + $selectedEntity = $this->selectEntity(static::CONTACTS_REPOSITORY, $entity->primaryPropertyValue()); + Assert::true($selectedEntity !== FALSE, "Contact entity with ID " . $entity->primaryPropertyValue() . "should be inserted in database"); + + $selectedEntity = $this->selectEntity(static::BUSINESS_CONTACTS_REPOSITORY, $entity->primaryPropertyValue()); + Assert::true($selectedEntity !== FALSE, "Contact entity with ID " . $entity->primaryPropertyValue() . "should be inserted in database"); + + Assert::exception( + function () use ($entity) { + $this->storage->insertEntity($entity); + }, + \obo\Exceptions\Exception::class + ); + } + public function testUpdateEntity() { $entity = $this->getContactEntity(); $newEmail = "test@test.com";