コード例 #1
0
 /**
  * Validates & completes a one-to-one association mapping.
  *
  * @param array $mapping  The mapping to validate & complete.
  * @throws RuntimeException
  * @throws MappingException
  * @return array The validated & completed mapping.@override
  */
 protected function _validateAndCompleteOneToOneMapping(array $mapping)
 {
     $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
     if (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
         $mapping['isOwningSide'] = true;
     }
     if ($mapping['isOwningSide']) {
         if (!isset($mapping['joinColumns']) || !$mapping['joinColumns']) {
             // Apply default join column
             $mapping['joinColumns'] = array(array('name' => $this->namingStrategy->joinColumnName($mapping['fieldName']), 'referencedColumnName' => $this->namingStrategy->referenceColumnName()));
         }
         $uniqueContraintColumns = array();
         foreach ($mapping['joinColumns'] as &$joinColumn) {
             if ($mapping['type'] === self::ONE_TO_ONE && !$this->isInheritanceTypeSingleTable()) {
                 if (count($mapping['joinColumns']) == 1) {
                     if (!isset($mapping['id']) || !$mapping['id']) {
                         $joinColumn['unique'] = true;
                     }
                 } else {
                     $uniqueContraintColumns[] = $joinColumn['name'];
                 }
             }
             if (empty($joinColumn['name'])) {
                 $joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName']);
             }
             if (empty($joinColumn['referencedColumnName'])) {
                 $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
             }
             if ($joinColumn['name'][0] === '`') {
                 $joinColumn['name'] = trim($joinColumn['name'], '`');
                 $joinColumn['quoted'] = true;
             }
             if ($joinColumn['referencedColumnName'][0] === '`') {
                 $joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`');
                 $joinColumn['quoted'] = true;
             }
             $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
             $mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName']) ? $joinColumn['fieldName'] : $joinColumn['name'];
         }
         if ($uniqueContraintColumns) {
             if (!$this->table) {
                 throw new RuntimeException("ClassMetadataInfo::setTable() has to be called before defining a one to one relationship.");
             }
             $this->table['uniqueConstraints'][$mapping['fieldName'] . "_uniq"] = array('columns' => $uniqueContraintColumns);
         }
         $mapping['targetToSourceKeyColumns'] = array_flip($mapping['sourceToTargetKeyColumns']);
     }
     $mapping['orphanRemoval'] = isset($mapping['orphanRemoval']) ? (bool) $mapping['orphanRemoval'] : false;
     $mapping['isCascadeRemove'] = $mapping['orphanRemoval'] ? true : $mapping['isCascadeRemove'];
     if ($mapping['orphanRemoval']) {
         unset($mapping['unique']);
     }
     if (isset($mapping['id']) && $mapping['id'] === true && !$mapping['isOwningSide']) {
         throw MappingException::illegalInverseIdentifierAssocation($this->name, $mapping['fieldName']);
     }
     return $mapping;
 }
 /**
  * Validates & completes a one-to-one association mapping.
  *
  * @param array $mapping  The mapping to validate & complete.
  * @return array The validated & completed mapping.
  * @override
  */
 protected function _validateAndCompleteOneToOneMapping(array $mapping)
 {
     $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
     if (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
         $mapping['isOwningSide'] = true;
     }
     if ($mapping['isOwningSide']) {
         if (!isset($mapping['joinColumns']) || !$mapping['joinColumns']) {
             // Apply default join column
             $mapping['joinColumns'] = array(array('name' => $mapping['fieldName'] . '_id', 'referencedColumnName' => 'id'));
         }
         $uniqueContraintColumns = array();
         foreach ($mapping['joinColumns'] as $key => &$joinColumn) {
             if ($mapping['type'] === self::ONE_TO_ONE) {
                 if (count($mapping['joinColumns']) == 1) {
                     $joinColumn['unique'] = true;
                 } else {
                     $uniqueContraintColumns[] = $joinColumn['name'];
                 }
             }
             if (empty($joinColumn['name'])) {
                 $joinColumn['name'] = $mapping['fieldName'] . '_id';
             }
             if (empty($joinColumn['referencedColumnName'])) {
                 $joinColumn['referencedColumnName'] = 'id';
             }
             $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
             $mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName']) ? $joinColumn['fieldName'] : $joinColumn['name'];
         }
         if ($uniqueContraintColumns) {
             if (!$this->table) {
                 throw new \RuntimeException("ClassMetadataInfo::setTable() has to be called before defining a one to one relationship.");
             }
             $this->table['uniqueConstraints'][$mapping['fieldName'] . "_uniq"] = array('columns' => $uniqueContraintColumns);
         }
         $mapping['targetToSourceKeyColumns'] = array_flip($mapping['sourceToTargetKeyColumns']);
     }
     //TODO: if orphanRemoval, cascade=remove is implicit!
     $mapping['orphanRemoval'] = isset($mapping['orphanRemoval']) ? (bool) $mapping['orphanRemoval'] : false;
     if (isset($mapping['id']) && $mapping['id'] === true && !$mapping['isOwningSide']) {
         throw MappingException::illegalInverseIdentifierAssocation($this->name, $mapping['fieldName']);
     }
     return $mapping;
 }