コード例 #1
0
ファイル: EntityPopulator.php プロジェクト: nbremont/Faker
 public function guessColumnFormatters(\Faker\Generator $generator)
 {
     $formatters = array();
     $class = $this->class;
     $nameGuesser = new \Faker\Guesser\Name($generator);
     $columnTypeGuesser = new ColumnTypeGuesser($generator);
     foreach ($this->class->getFieldNames() as $fieldName) {
         if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
             continue;
         }
         if ($formatter = $nameGuesser->guessFormat($fieldName)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
         if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
     }
     foreach ($this->class->getAssociationNames() as $assocName) {
         if (!$this->class->isIdentifier($assocName) || !$this->class->isCollectionValuedAssociation($assocName)) {
             continue;
         }
         $relatedClass = $this->class->getAssociationTargetClass($assocName);
         $formatters[$assocName] = function ($inserted) use($relatedClass) {
             return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null;
         };
     }
     return $formatters;
 }
コード例 #2
0
ファイル: DoctrineObject.php プロジェクト: eltondias/Relogio
 /**
  * Extract values from an object using a by-reference logic (this means that values are
  * directly fetched without using the public API of the entity, in this case, getters)
  *
  * @param  object $object
  * @return array
  */
 protected function extractByReference($object)
 {
     $fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
     $refl = $this->metadata->getReflectionClass();
     $data = array();
     foreach ($fieldNames as $fieldName) {
         $reflProperty = $refl->getProperty($fieldName);
         $reflProperty->setAccessible(true);
         $data[$fieldName] = $this->extractValue($fieldName, $reflProperty->getValue($object));
     }
     return $data;
 }
コード例 #3
0
 /**
  * Extract values from an object using a by-reference logic (this means that values are
  * directly fetched without using the public API of the entity, in this case, getters)
  *
  * @param  object $object
  * @return array
  */
 protected function extractByReference($object)
 {
     $fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
     $refl = $this->metadata->getReflectionClass();
     $filter = $object instanceof FilterProviderInterface ? $object->getFilter() : $this->filterComposite;
     $data = array();
     foreach ($fieldNames as $fieldName) {
         if ($filter && !$filter->filter($fieldName)) {
             continue;
         }
         $reflProperty = $refl->getProperty($fieldName);
         $reflProperty->setAccessible(true);
         $data[$fieldName] = $this->extractValue($fieldName, $reflProperty->getValue($object), $object);
     }
     return $data;
 }
コード例 #4
0
 protected function addAssociationsTargetClasses(ClassMetadata $metadata, array &$classes)
 {
     $associationNames = $metadata->getAssociationNames();
     foreach ($associationNames as $associationName) {
         $associationClass = $metadata->getAssociationTargetClass($associationName);
         $classes[$associationClass] = $associationClass;
     }
 }
コード例 #5
0
 /**
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata
  * @param                                                    $model
  *
  * @return array
  */
 protected function processAssociationNames(ClassMetadata $metadata, $model)
 {
     $data = array();
     foreach ($metadata->getAssociationNames() as $assocName) {
         $propertyValue = $this->readProperty($model, $assocName);
         if (null === $propertyValue || $metadata->isAssociationInverseSide($assocName)) {
             continue;
         }
         if ($metadata->isSingleValuedAssociation($assocName)) {
             $assocValue = $this->namingStrategy->modelName($propertyValue, $this->getManager()->getClassMetadata(get_class($propertyValue)));
             $assocValue = $this->navigator->accept($this->getVisitor(), $assocValue, 'reference');
             $data[$assocName] = $assocValue;
         } else {
             $data[$assocName] = array();
             foreach ($propertyValue as $value) {
                 $assocValue = $this->namingStrategy->modelName($value, $this->getManager()->getClassMetadata(get_class($value)));
                 $assocValue = $this->navigator->accept($this->getVisitor(), $assocValue, 'reference');
                 $data[$assocName][] = $assocValue;
             }
         }
     }
     return $data;
 }
コード例 #6
0
 protected function addDependentFixtureInterface(PhpClass $class, ClassMetadata $metadata, array $options)
 {
     $class->addInterfaceName('Doctrine\\Common\\DataFixtures\\DependentFixtureInterface');
     $writer = new Writer();
     $method = PhpMethod::create('getDependencies');
     $writer->writeln("return array(");
     $associations = array();
     foreach ($metadata->getAssociationNames() as $assocName) {
         $targetClass = $metadata->getAssociationTargetClass($assocName);
         $associations[] = sprintf("'%s\\%s'", $options['namespace'], $this->namingStrategy->fixtureName($this->getManager()->getClassMetadata($targetClass)));
     }
     $writer->indent();
     $writer->writeln(implode(",\n", $associations));
     $writer->outdent();
     $writer->writeln(");");
     $method->setBody($writer->getContent());
     $class->setMethod($method);
 }
コード例 #7
0
 /**
  * Returns the entity fields
  *
  * @param ClassMetadata $metadata
  *
  * @return array
  */
 protected function getEntityAssociations(ClassMetadata $metadata)
 {
     $entityAssociations = $metadata->getAssociationNames();
     $associations = [];
     foreach ($entityAssociations as $association) {
         $associations[$association] = $association;
     }
     return $associations;
 }
コード例 #8
0
 private function prepareMetadataAssociations(ClassMetadata $metadata)
 {
     $associations = [];
     foreach ($metadata->getAssociationNames() as $associationName) {
         $associations[$associationName]['groups'] = [Inflector::tableize($metadata->getReflectionClass()->getShortName())];
     }
     return $associations;
 }
コード例 #9
0
 /**
  * Производит простой перебор ассоциаций, взятых из метаданных и на каждую ассоциацию вызывает callback
  *
  * @param ClassMetadata $metadata мета данные сущности
  * @param callable $callback метод, который будет вызван для каждой найденной ассоциации
  */
 private function bypassAssociationsOfClass(ClassMetadata $metadata, \Closure $callback)
 {
     foreach ($metadata->getAssociationNames() as $assocName) {
         if (!$metadata->isCollectionValuedAssociation($assocName)) {
             $callback($assocName);
         }
     }
 }
コード例 #10
0
 /**
  * @param ClassMetadata $class1
  * @param ClassMetadata $class2
  * @return string|null
  */
 private function getClassReverseAssociationName(ClassMetadata $class1, ClassMetadata $class2)
 {
     foreach ($class2->getAssociationNames() as $class2Side) {
         $targetClass = $this->getClassByName($class2->getAssociationTargetClass($class2Side));
         if ($class1->getName() === $targetClass->getName()) {
             return $class2Side;
         }
     }
     return null;
 }
コード例 #11
0
ファイル: EntityPopulator.php プロジェクト: brainrepo/Faker
 /**
  * @param \Faker\Generator $generator
  * @return array
  */
 public function guessColumnFormatters(\Faker\Generator $generator)
 {
     $formatters = array();
     $nameGuesser = new \Faker\Guesser\Name($generator);
     $columnTypeGuesser = new ColumnTypeGuesser($generator);
     foreach ($this->class->getFieldNames() as $fieldName) {
         if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
             continue;
         }
         $size = isset($this->class->fieldMappings[$fieldName]['length']) ? $this->class->fieldMappings[$fieldName]['length'] : null;
         if ($formatter = $nameGuesser->guessFormat($fieldName, $size)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
         if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
     }
     foreach ($this->class->getAssociationNames() as $assocName) {
         if ($this->class->isCollectionValuedAssociation($assocName)) {
             continue;
         }
         $relatedClass = $this->class->getAssociationTargetClass($assocName);
         $unique = $optional = false;
         if ($this->class instanceof \Doctrine\ORM\Mapping\ClassMetadata) {
             $mappings = $this->class->getAssociationMappings();
             foreach ($mappings as $mapping) {
                 if ($mapping['targetEntity'] == $relatedClass) {
                     if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_ONE) {
                         $unique = true;
                         $optional = isset($mapping['joinColumns'][0]['nullable']) ? $mapping['joinColumns'][0]['nullable'] : false;
                         break;
                     }
                 }
             }
         } elseif ($this->class instanceof \Doctrine\ODM\MongoDB\Mapping\ClassMetadata) {
             $mappings = $this->class->associationMappings;
             foreach ($mappings as $mapping) {
                 if ($mapping['targetDocument'] == $relatedClass) {
                     if ($mapping['type'] == \Doctrine\ODM\MongoDB\Mapping\ClassMetadata::ONE && $mapping['association'] == \Doctrine\ODM\MongoDB\Mapping\ClassMetadata::REFERENCE_ONE) {
                         $unique = true;
                         $optional = isset($mapping['nullable']) ? $mapping['nullable'] : false;
                         break;
                     }
                 }
             }
         }
         $index = 0;
         $formatters[$assocName] = function ($inserted) use($relatedClass, &$index, $unique, $optional) {
             if (isset($inserted[$relatedClass])) {
                 if ($unique) {
                     $related = null;
                     if (isset($inserted[$relatedClass][$index]) || !$optional) {
                         $related = $inserted[$relatedClass][$index];
                     }
                     $index++;
                     return $related;
                 }
                 return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
             }
             return null;
         };
     }
     return $formatters;
 }