1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-24 09:19:50 +01:00

Deal with Doctrine entity having a one-to-one nullable relationship

This commit is contained in:
Jonathan Petitcolas 2013-10-23 17:26:41 +02:00
parent 728dc177d5
commit 5cca368718

View File

@ -99,21 +99,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)];
}