1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-21 15:59:52 +01:00

Merge pull request #197 from jpetitcolas/optional_onetoone

[WIP] Issue with one-to-one nullable relationship
This commit is contained in:
Francois Zaninotto 2014-01-06 09:50:11 -08:00
commit ff43a34c9c

View File

@ -100,21 +100,29 @@ class EntityPopulator
$relatedClass = $this->class->getAssociationTargetClass($assocName);
$unique = false;
$unique = $optional = false;
$mappings = $this->class->getAssociationMappings();
foreach ($mappings as $mapping) {
if ($mapping['targetEntity'] == $relatedClass) {
if ($mapping['type'] == ClassMetadata::ONE_TO_ONE) {
$unique = true;
$optional = $mapping['joinColumns'][0]['nullable'];
break;
}
}
}
$index = 0;
$formatters[$assocName] = function($inserted) use ($relatedClass, &$index, $unique) {
$formatters[$assocName] = function($inserted) use ($relatedClass, &$index, $unique, $optional) {
if ($unique && isset($inserted[$relatedClass])) {
return $inserted[$relatedClass][$index++];
$related = null;
if (isset($inserted[$relatedClass][$index]) || !$optional) {
$related = $inserted[$relatedClass][$index];
}
$index++;
return $related;
} elseif (isset($inserted[$relatedClass])) {
return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
}