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:
commit
ff43a34c9c
@ -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)];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user