/**
  * Finds an object by its primary key / identifier.
  *
  * @param mixed $id The identifier.
  *
  * @return object The object.
  */
 public function find($id)
 {
     if (!is_array($id)) {
         $idFields = $this->classMetadata->getIdentifierFieldNames();
         $idField = reset($idFields);
         $id = [$idField => $id];
     }
     return $this->persister->loadById($id);
 }
 /**
  * Creates a new entity choice list.
  *
  * @param ObjectManager         $manager      An EntityManager instance
  * @param string                $class        The class name
  * @param string                $labelPath    The property path used for the label
  * @param EntityLoaderInterface $entityLoader An optional query builder
  * @param array                 $entities     An array of choices
  * @param string                $groupPath    A property path pointing to the property used
  *                                            to group the choices. Only allowed if
  *                                            the choices are given as flat array.
  */
 public function __construct(ObjectManager $manager, $class, $labelPath = null, EntityLoaderInterface $entityLoader = null, $entities = null, $groupPath = null)
 {
     $this->em = $manager;
     $this->entityLoader = $entityLoader;
     $this->classMetadata = $manager->getClassMetadata($class);
     $this->class = $this->classMetadata->getName();
     $this->identifier = $this->classMetadata->getIdentifierFieldNames();
     $this->loaded = is_array($entities) || $entities instanceof \Traversable;
     if (!$this->loaded) {
         // Make sure the constraints of the parent constructor are
         // fulfilled
         $entities = array();
     }
     parent::__construct($entities, $labelPath, array(), $groupPath);
 }
Example #3
0
 /**
  * Creates a new entity choice list.
  *
  * @param ObjectManager         $manager      An EntityManager instance
  * @param string                $class        The class name
  * @param string                $labelPath    The property path used for the label
  * @param EntityLoaderInterface $entityLoader An optional query builder
  * @param array                 $entities     An array of choices
  * @param string                $groupPath    A property path pointing to the property used
  *                                            to group the choices. Only allowed if
  *                                            the choices are given as flat array.
  */
 public function __construct(ObjectManager $manager, $class, $labelPath = null, EntityLoaderInterface $entityLoader = null, $entities = null, $groupPath = null)
 {
     $this->em = $manager;
     $this->entityLoader = $entityLoader;
     $this->classMetadata = $manager->getClassMetadata($class);
     $this->class = $this->classMetadata->getName();
     $this->loaded = is_array($entities) || $entities instanceof \Traversable;
     $identifier = $this->classMetadata->getIdentifierFieldNames();
     if (1 === count($identifier)) {
         $this->idField = $identifier[0];
         $this->idAsValue = true;
         if ('integer' === $this->classMetadata->getTypeOfField($this->idField)) {
             $this->idAsIndex = true;
         }
     }
     if (!$this->loaded) {
         // Make sure the constraints of the parent constructor are
         // fulfilled
         $entities = array();
     }
     if (version_compare(Kernel::VERSION, '2.1') <= 0) {
         $this->labelPath = $labelPath ? new DepPropertyPath($labelPath) : null;
         $this->groupPath = $groupPath ? new DepPropertyPath($groupPath) : null;
     } else {
         $this->labelPath = $labelPath ? new PropertyPath($labelPath) : null;
         $this->groupPath = $groupPath ? new PropertyPath($groupPath) : null;
     }
     parent::__construct($entities, array(), array());
 }
 /**
  * Get default order by.
  *
  * @return array|null
  */
 protected function getDefaultOrderBy()
 {
     $ids = $this->metadata->getIdentifierFieldNames();
     $orderBy = $ids ? [] : null;
     foreach ($ids as $pk) {
         $orderBy[$pk] = 'ASC';
     }
     return $orderBy;
 }
Example #5
0
 public function __construct(ObjectManager $om, ClassMetadata $classMetadata)
 {
     $ids = $classMetadata->getIdentifierFieldNames();
     $idType = $classMetadata->getTypeOfField(current($ids));
     $this->om = $om;
     $this->classMetadata = $classMetadata;
     $this->singleId = 1 === count($ids);
     $this->intId = $this->singleId && in_array($idType, array('integer', 'smallint', 'bigint'));
     $this->idField = current($ids);
 }
 /**
  * This function tries, given an array of data, to convert it to an object if the given array contains
  * an identifier for the object. This is useful in a context of updating existing entities, without ugly
  * tricks like setting manually the existing id directly into the entity
  *
  * @param  array  $data
  * @param  object $object
  * @return object
  */
 protected function tryConvertArrayToObject($data, $object)
 {
     $identifierNames = $this->metadata->getIdentifierFieldNames($object);
     $identifierValues = array();
     if (empty($identifierNames)) {
         return $object;
     }
     foreach ($identifierNames as $identifierName) {
         if (!isset($data[$identifierName]) || empty($data[$identifierName])) {
             return $object;
         }
         $identifierValues[$identifierName] = $data[$identifierName];
     }
     return $this->find(get_class($object), $identifierValues);
 }
 public function __construct(ObjectManager $om, ClassMetadata $classMetadata)
 {
     $ids = $classMetadata->getIdentifierFieldNames();
     $idType = $classMetadata->getTypeOfField(current($ids));
     $this->om = $om;
     $this->classMetadata = $classMetadata;
     $this->singleId = 1 === count($ids);
     $this->intId = $this->singleId && in_array($idType, array('integer', 'smallint', 'bigint'));
     $this->idField = current($ids);
     // single field association are resolved, since the schema column could be an int
     if ($this->singleId && $classMetadata->hasAssociation($this->idField)) {
         $this->associationIdReader = new self($om, $om->getClassMetadata($classMetadata->getAssociationTargetClass($this->idField)));
         $this->singleId = $this->associationIdReader->isSingleId();
         $this->intId = $this->associationIdReader->isIntId();
     }
 }
Example #8
0
 /**
  * Constructor.
  *
  * @param ObjectManager         $manager           An EntityManager instance
  * @param string                $class        The class name
  * @param string                $property     The property name
  * @param EntityLoaderInterface $entityLoader An optional query builder
  * @param array|\Closure        $choices      An array of choices or a function returning an array
  * @param string                $groupBy
  */
 public function __construct(ObjectManager $manager, $class, $property = null, EntityLoaderInterface $entityLoader = null, $choices = null, $groupBy = null)
 {
     $this->em = $manager;
     $this->class = $class;
     $this->entityLoader = $entityLoader;
     $this->classMetadata = $manager->getClassMetadata($class);
     $this->identifier = $this->classMetadata->getIdentifierFieldNames();
     $this->groupBy = $groupBy;
     // The property option defines, which property (path) is used for
     // displaying entities as strings
     if ($property) {
         $this->propertyPath = new PropertyPath($property);
     } elseif (!method_exists($this->class, '__toString')) {
         // Otherwise expect a __toString() method in the entity
         throw new FormException('Entities passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).');
     }
     if (!is_array($choices) && !$choices instanceof \Closure && !is_null($choices)) {
         throw new UnexpectedTypeException($choices, 'array or \\Closure or null');
     }
     $this->choices = $choices;
 }
 /**
  * Creates a new entity choice list.
  *
  * @param ObjectManager             $manager           An EntityManager instance
  * @param string                    $class             The class name
  * @param string                    $labelPath         The property path used for the label
  * @param EntityLoaderInterface     $entityLoader      An optional query builder
  * @param array                     $entities          An array of choices
  * @param array                     $preferredEntities An array of preferred choices
  * @param string                    $groupPath         A property path pointing to the property used
  *                                                     to group the choices. Only allowed if
  *                                                     the choices are given as flat array.
  * @param PropertyAccessorInterface $propertyAccessor  The reflection graph for reading property paths.
  */
 public function __construct(ObjectManager $manager, $class, $labelPath = null, EntityLoaderInterface $entityLoader = null, $entities = null, array $preferredEntities = array(), $groupPath = null, PropertyAccessorInterface $propertyAccessor = null)
 {
     $this->em = $manager;
     $this->entityLoader = $entityLoader;
     $this->classMetadata = $manager->getClassMetadata($class);
     $this->class = $this->classMetadata->getName();
     $this->loaded = is_array($entities) || $entities instanceof \Traversable;
     $this->preferredEntities = $preferredEntities;
     $identifier = $this->classMetadata->getIdentifierFieldNames();
     if (1 === count($identifier)) {
         $this->idField = $identifier[0];
         $this->idAsValue = true;
         if (in_array($this->classMetadata->getTypeOfField($this->idField), array('integer', 'smallint', 'bigint'))) {
             $this->idAsIndex = true;
         }
     }
     if (!$this->loaded) {
         // Make sure the constraints of the parent constructor are
         // fulfilled
         $entities = array();
     }
     parent::__construct($entities, $labelPath, $preferredEntities, $groupPath, null, $propertyAccessor);
 }
Example #10
0
 /**
  * Get identifier information for a class.
  *
  * @param ClassMetadata $classMetadata The entity metadata
  *
  * @return array Return an array with idAsIndex, idAsValue and identifier
  */
 private function getIdentifierInfoForClass(ClassMetadata $classMetadata)
 {
     $identifier = null;
     $idAsIndex = false;
     $idAsValue = false;
     $identifiers = $classMetadata->getIdentifierFieldNames();
     if (1 === count($identifiers)) {
         $identifier = $identifiers[0];
         if (!$classMetadata->hasAssociation($identifier)) {
             $idAsValue = true;
             if (in_array($classMetadata->getTypeOfField($identifier), array('integer', 'smallint', 'bigint'))) {
                 $idAsIndex = true;
             }
         }
     }
     return array($idAsIndex, $idAsValue, $identifier);
 }