Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions tests/MySQLTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down