コード例 #1
0
ファイル: ClassMetadata.php プロジェクト: mike227/n-forms
 /** {@inheritdoc} */
 public function getId($object)
 {
     if (!is_a($object, $this->getClass())) {
         throw new MetadataException("Object isn't subclass of '{$this->getClass()}', given '" . (is_object($object) ? get_class($object) : gettype($object)) . "'.");
     }
     $identifier = $this->classMetadata->getIdentifierValues($object);
     if (empty($identifier)) {
         return NULL;
     }
     return is_array($identifier) ? implode('_', $identifier) : $identifier;
 }
コード例 #2
0
 /**
  * Executes all queued entity insertions and returns any generated post-insert
  * identifiers that were created as a result of the insertions.
  * 
  * If no inserts are queued, invoking this method is a NOOP.
  *
  * @return array An array of any generated post-insert IDs. This will be an empty array
  *               if the entity class does not use the IDENTITY generation strategy.
  */
 public function executeInserts()
 {
     if (!$this->_queuedInserts) {
         return;
     }
     $postInsertIds = array();
     $idGen = $this->_class->idGenerator;
     $isPostInsertId = $idGen->isPostInsertGenerator();
     $stmt = $this->_conn->prepare($this->getInsertSQL());
     $tableName = $this->_class->table['name'];
     foreach ($this->_queuedInserts as $entity) {
         $insertData = $this->_prepareInsertData($entity);
         if (isset($insertData[$tableName])) {
             $paramIndex = 1;
             foreach ($insertData[$tableName] as $column => $value) {
                 $stmt->bindValue($paramIndex++, $value, $this->_columnTypes[$column]);
             }
         }
         $stmt->execute();
         if ($isPostInsertId) {
             $id = $idGen->generate($this->_em, $entity);
             $postInsertIds[$id] = $entity;
         } else {
             $id = $this->_class->getIdentifierValues($entity);
         }
         if ($this->_class->isVersioned) {
             $this->_assignDefaultVersionValue($this->_class, $entity, $id);
         }
     }
     $stmt->closeCursor();
     $this->_queuedInserts = array();
     return $postInsertIds;
 }
コード例 #3
0
 /**
  * Checks whether the given managed entity exists in the database.
  *
  * @param object $entity
  * @return boolean TRUE if the entity exists in the database, FALSE otherwise.
  */
 public function exists($entity, array $extraConditions = array())
 {
     $criteria = $this->_class->getIdentifierValues($entity);
     if ($extraConditions) {
         $criteria = array_merge($criteria, $extraConditions);
     }
     $sql = 'SELECT 1 ' . $this->getLockTablesSql() . ' WHERE ' . $this->_getSelectConditionSQL($criteria);
     return (bool) $this->_conn->fetchColumn($sql, array_values($criteria));
 }
コード例 #4
0
 /**
  * Checks whether the given managed entity exists in the database.
  *
  * @param object $entity
  * @return boolean TRUE if the entity exists in the database, FALSE otherwise.
  */
 public function exists($entity, array $extraConditions = array())
 {
     $criteria = $this->_class->getIdentifierValues($entity);
     if ($extraConditions) {
         $criteria = array_merge($criteria, $extraConditions);
     }
     $sql = 'SELECT 1 FROM ' . $this->_class->getQuotedTableName($this->_platform) . ' ' . $this->_getSQLTableAlias($this->_class->name) . ' WHERE ' . $this->_getSelectConditionSQL($criteria);
     return (bool) $this->_conn->fetchColumn($sql, array_values($criteria));
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 public function exists($entity, Criteria $extraConditions = null)
 {
     if (null === $extraConditions) {
         $key = new EntityCacheKey($this->class->rootEntityName, $this->class->getIdentifierValues($entity));
         if ($this->region->contains($key)) {
             return true;
         }
     }
     return $this->persister->exists($entity, $extraConditions);
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function exists($entity, array $extraConditions = array())
 {
     if (empty($extraConditions)) {
         $key = new EntityCacheKey($this->class->rootEntityName, $this->class->getIdentifierValues($entity));
         if ($this->region->contains($key)) {
             return true;
         }
     }
     return $this->persister->exists($entity, $extraConditions);
 }
コード例 #7
0
 /**
  * {@inheritdoc}
  */
 public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, $entity)
 {
     $data = $this->uow->getOriginalEntityData($entity);
     $data = array_merge($data, $metadata->getIdentifierValues($entity));
     // why update has no identifier values ?
     foreach ($metadata->associationMappings as $name => $assoc) {
         if (!isset($data[$name])) {
             continue;
         }
         if (!($assoc['type'] & ClassMetadata::TO_ONE)) {
             unset($data[$name]);
             continue;
         }
         if (!isset($assoc['cache'])) {
             $targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']);
             $owningAssociation = !$assoc['isOwningSide'] ? $targetClassMetadata->associationMappings[$assoc['mappedBy']] : $assoc;
             $associationIds = $this->identifierFlattener->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($data[$name]));
             unset($data[$name]);
             foreach ($associationIds as $fieldName => $fieldValue) {
                 if (isset($targetClassMetadata->fieldMappings[$fieldName])) {
                     $fieldMapping = $targetClassMetadata->fieldMappings[$fieldName];
                     $data[$owningAssociation['targetToSourceKeyColumns'][$fieldMapping['columnName']]] = $fieldValue;
                     continue;
                 }
                 $targetAssoc = $targetClassMetadata->associationMappings[$fieldName];
                 foreach ($assoc['targetToSourceKeyColumns'] as $referencedColumn => $localColumn) {
                     if (isset($targetAssoc['sourceToTargetKeyColumns'][$referencedColumn])) {
                         $data[$localColumn] = $fieldValue;
                     }
                 }
             }
             continue;
         }
         if (!isset($assoc['id'])) {
             $targetClass = ClassUtils::getClass($data[$name]);
             $targetId = $this->uow->getEntityIdentifier($data[$name]);
             $data[$name] = new AssociationCacheEntry($targetClass, $targetId);
             continue;
         }
         // handle association identifier
         $targetId = is_object($data[$name]) && $this->uow->isInIdentityMap($data[$name]) ? $this->uow->getEntityIdentifier($data[$name]) : $data[$name];
         // @TODO - fix it !
         // handle UnitOfWork#createEntity hash generation
         if (!is_array($targetId)) {
             $data[reset($assoc['joinColumnFieldNames'])] = $targetId;
             $targetEntity = $this->em->getClassMetadata($assoc['targetEntity']);
             $targetId = [$targetEntity->identifier[0] => $targetId];
         }
         $data[$name] = new AssociationCacheEntry($assoc['targetEntity'], $targetId);
     }
     return new EntityCacheEntry($metadata->name, $data);
 }
コード例 #8
0
 /**
  * Checks whether the given managed entity exists in the database.
  *
  * @param object $entity
  * @return boolean TRUE if the entity exists in the database, FALSE otherwise.
  */
 public function exists($entity, array $extraConditions = array())
 {
     $criteria = $this->_class->getIdentifierValues($entity);
     if ($extraConditions) {
         $criteria = array_merge($criteria, $extraConditions);
     }
     $alias = $this->_getSQLTableAlias($this->_class->name);
     $sql = 'SELECT 1 ' . $this->getLockTablesSql() . ' WHERE ' . $this->_getSelectConditionSQL($criteria);
     if ($filterSql = $this->generateFilterConditionSQL($this->_class, $alias)) {
         $sql .= ' AND ' . $filterSql;
     }
     list($params, $types) = $this->expandParameters($criteria);
     return (bool) $this->_conn->fetchColumn($sql, $params);
 }
コード例 #9
0
 /**
  * @param object $object
  * @param ClassMetadata $objectMetadata
  *
  * @return RecipientEntity
  */
 public function createRecipientEntity($object, ClassMetadata $objectMetadata)
 {
     $identifiers = $objectMetadata->getIdentifierValues($object);
     if (count($identifiers) !== 1) {
         return null;
     }
     $organizationName = null;
     if ($this->getPropertyAccessor()->isReadable($object, static::ORGANIZATION_PROPERTY)) {
         $organization = $this->getPropertyAccessor()->getValue($object, static::ORGANIZATION_PROPERTY);
         if ($organization) {
             $organizationName = $organization->getName();
         }
     }
     return new RecipientEntity($objectMetadata->name, reset($identifiers), $this->createRecipientEntityLabel($this->nameFormatter->format($object), $objectMetadata->name), $organizationName);
 }
コード例 #10
0
 /**
  * Executes all queued inserts.
  *
  * @return array An array of any generated post-insert IDs.
  */
 public function executeInserts()
 {
     if (!$this->_queuedInserts) {
         return;
     }
     $isVersioned = $this->_class->isVersioned;
     $postInsertIds = array();
     $idGen = $this->_class->idGenerator;
     $isPostInsertId = $idGen->isPostInsertGenerator();
     $stmt = $this->_conn->prepare($this->getInsertSQL());
     $primaryTableName = $this->_class->primaryTable['name'];
     foreach ($this->_queuedInserts as $entity) {
         $insertData = array();
         $this->_prepareData($entity, $insertData, true);
         if (isset($insertData[$primaryTableName])) {
             $paramIndex = 1;
             if ($this->_sqlLogger !== null) {
                 $params = array();
                 foreach ($insertData[$primaryTableName] as $value) {
                     $params[$paramIndex] = $value;
                     $stmt->bindValue($paramIndex++, $value);
                 }
                 $this->_sqlLogger->logSql($this->getInsertSQL(), $params);
             } else {
                 foreach ($insertData[$primaryTableName] as $value) {
                     $stmt->bindValue($paramIndex++, $value);
                 }
             }
         } else {
             if ($this->_sqlLogger !== null) {
                 $this->_sqlLogger->logSql($this->getInsertSQL());
             }
         }
         $stmt->execute();
         if ($isPostInsertId) {
             $id = $idGen->generate($this->_em, $entity);
             $postInsertIds[$id] = $entity;
         } else {
             $id = $this->_class->getIdentifierValues($entity);
         }
         if ($isVersioned) {
             $this->_assignDefaultVersionValue($this->_class, $entity, $id);
         }
     }
     $stmt->closeCursor();
     $this->_queuedInserts = array();
     return $postInsertIds;
 }
コード例 #11
0
 /**
  * {@inheritdoc}
  */
 public function exists($entity, Criteria $extraConditions = null)
 {
     $criteria = $this->class->getIdentifierValues($entity);
     if (!$criteria) {
         return false;
     }
     $alias = $this->getSQLTableAlias($this->class->name);
     $sql = 'SELECT 1 ' . $this->getLockTablesSql(null) . ' WHERE ' . $this->getSelectConditionSQL($criteria);
     list($params, $types) = $this->expandParameters($criteria);
     if (null !== $extraConditions) {
         $sql .= ' AND ' . $this->getSelectConditionCriteriaSQL($extraConditions);
         list($criteriaParams, $criteriaTypes) = $this->expandCriteriaParameters($extraConditions);
         $params = array_merge($params, $criteriaParams);
         $types = array_merge($types, $criteriaTypes);
     }
     if ($filterSql = $this->generateFilterConditionSQL($this->class, $alias)) {
         $sql .= ' AND ' . $filterSql;
     }
     return (bool) $this->conn->fetchColumn($sql, $params, 0, $types);
 }
 /**
  * @param ClassMetaData $association_meta
  * @param string        $left
  * @param string        $right
  * @return bool
  */
 private function hasAssociationChanged(ClassMetadata $association_meta, $left, $right)
 {
     // check if the PK of the related entity has changed (thus different link)
     if (null !== $left && null !== $right) {
         $left_values = $association_meta->getIdentifierValues($left);
         $right_values = $association_meta->getIdentifierValues($right);
         $diff = array_udiff($left_values, $right_values, function ($a, $b) {
             // note that equal returns 0, difference should return -1 or 1
             if (!is_object($a) && !is_object($b)) {
                 return (string) $a === (string) $b ? 0 : 1;
             }
             // prevent casting objects to strings
             return $a === $b ? 0 : 1;
         });
         if (!empty($diff)) {
             $this->logger->info('Association Change detected on owning ONE side', ['left' => $left_values, 'right' => $right_values]);
             return true;
         }
     }
     return $left != $right;
 }
コード例 #13
0
 function it_should_throw_an_exception_if_there_is_not_exactly_one_identity_value(DefinitionInterface $definition, ClassMetadata $metadata, \stdClass $object)
 {
     $metadata->getIdentifierValues($object)->willReturn([1, 2, 3]);
     $this->shouldThrow('Bravesheep\\CrudifyBundle\\Exception\\UnsupportedEntityException')->duringGetId($definition, $object);
 }