Пример #1
0
 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
 protected function getEntityColumns(ClassMetadata $metaData)
 {
     $columns = $metaData->getColumnNames();
     $result = array();
     foreach ($columns as $columnName) {
         $type = $this->getColumnTypeValue($metaData->getTypeOfField($columnName));
         if ($metaData->isIdentifier($metaData->getFieldName($columnName))) {
             $type |= TableInterface::PRIMARY_KEY;
             if ($metaData->isIdGeneratorIdentity() || $metaData->isIdGeneratorSequence()) {
                 $type |= TableInterface::AUTO_INCREMENT;
             }
         }
         $result[$columnName] = $type;
     }
     return $result;
 }
Пример #3
0
 /**
  * Sets a persistent fields value.
  *
  * @param string $field
  * @param array  $args
  *
  * @return void
  *
  * @throws \BadMethodCallException   When no persistent field exists by that name.
  * @throws \InvalidArgumentException When the wrong target object type is passed to an association.
  */
 private function set($field, $args)
 {
     $this->initializeDoctrine();
     if ($this->cm->hasField($field) && !$this->cm->isIdentifier($field)) {
         $this->{$field} = $args[0];
     } else {
         if ($this->cm->hasAssociation($field) && $this->cm->isSingleValuedAssociation($field)) {
             $targetClass = $this->cm->getAssociationTargetClass($field);
             if (!$args[0] instanceof $targetClass && $args[0] !== null) {
                 throw new \InvalidArgumentException("Expected persistent object of type '" . $targetClass . "'");
             }
             $this->{$field} = $args[0];
             $this->completeOwningSide($field, $targetClass, $args[0]);
         } else {
             throw new \BadMethodCallException("no field with name '" . $field . "' exists on '" . $this->cm->getName() . "'");
         }
     }
 }
 /**
  * Generates the list of public properties to be lazy loaded, with their default values.
  *
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
  *
  * @return mixed[]
  */
 private function getLazyLoadedPublicProperties(ClassMetadata $class)
 {
     $defaultProperties = $class->getReflectionClass()->getDefaultProperties();
     $properties = array();
     foreach ($class->getReflectionClass()->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
         $name = $property->getName();
         if (($class->hasField($name) || $class->hasAssociation($name)) && !$class->isIdentifier($name)) {
             $properties[$name] = $defaultProperties[$name];
         }
     }
     return $properties;
 }
Пример #5
0
 /**
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata
  * @param                                                    $model
  *
  * @return array
  */
 protected function processFieldNames(ClassMetadata $metadata, $model)
 {
     $data = array();
     foreach ($metadata->getFieldNames() as $fieldName) {
         if ($metadata->isIdentifier($fieldName) && $metadata->usesIdGenerator()) {
             continue;
         }
         $data[$fieldName] = $this->navigator->accept($this->getVisitor(), $this->readProperty($model, $fieldName));
     }
     return $data;
 }
Пример #6
0
 /**
  * @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;
 }