ClassMetadata.php 11.8 KB
Newer Older
1
<?php
romanb's avatar
romanb committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
20
 */
21

22
namespace Doctrine\ORM\Mapping;
23

24
/**
25 26
 * A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
 * of an entity and it's associations.
27 28
 * 
 * Once populated, ClassMetadata instances are usually cached in a serialized form.
29
 *
30 31 32 33 34 35 36 37
 * <b>IMPORTANT NOTE:</b>
 *
 * The fields of this class are only public for 2 reasons:
 * 1) To allow fast READ access.
 * 2) To drastically reduce the size of a serialized instance (private/protected members
 *    get the whole class name, namespace inclusive, prepended to every property in
 *    the serialized representation).
 *
38
 * @author Roman Borschel <roman@code-factory.org>
39
 * @author Jonathan H. Wage <jonwage@gmail.com>
40
 * @since 2.0
41
 */
romanb's avatar
romanb committed
42
class ClassMetadata extends ClassMetadataInfo
43
{
44 45 46 47 48
    /**
     * The ReflectionClass instance of the mapped class.
     *
     * @var ReflectionClass
     */
49
    public $reflClass;
50 51 52 53 54 55

    /**
     * The ReflectionProperty instances of the mapped class.
     *
     * @var array
     */
56
    public $reflFields = array();
57 58 59 60 61 62 63
    
    /**
     * The prototype from which new instances of the mapped class are created.
     * 
     * @var object
     */
    private $_prototype;
64

65
    /**
66 67
     * Initializes a new ClassMetadata instance that will hold the object-relational mapping
     * metadata of the class with the given name.
68
     *
69
     * @param string $entityName The name of the entity class the new instance is used for.
70
     */
71
    public function __construct($entityName)
72
    {
73
        $this->name = $entityName;
romanb's avatar
romanb committed
74
        $this->reflClass = new \ReflectionClass($entityName);
75 76 77
        $this->namespace = $this->reflClass->getNamespaceName();
        $this->primaryTable['name'] = $this->reflClass->getShortName();
        $this->rootEntityName = $entityName;
78
    }
79

80 81 82 83 84
    /**
     * Gets the ReflectionClass instance of the mapped class.
     *
     * @return ReflectionClass
     */
85 86
    public function getReflectionClass()
    {
87
        return $this->reflClass;
88 89
    }

90 91 92 93 94
    /**
     * Gets the ReflectionPropertys of the mapped class.
     *
     * @return array An array of ReflectionProperty instances.
     */
95 96
    public function getReflectionProperties()
    {
97
        return $this->reflFields;
98 99
    }

100 101 102 103 104 105 106 107 108
    /**
     * INTERNAL:
     * Adds a reflection property. Usually only used by the ClassMetadataFactory
     * while processing inheritance mappings.
     *
     * @param array $props
     */
    public function addReflectionProperty($propName, \ReflectionProperty $property)
    {
109
        $this->reflFields[$propName] = $property;
110 111
    }

112 113 114 115 116 117
    /**
     * Gets a ReflectionProperty for a specific field of the mapped class.
     *
     * @param string $name
     * @return ReflectionProperty
     */
118
    public function getReflectionProperty($name)
romanb's avatar
romanb committed
119
    {
120
        return $this->reflFields[$name];
romanb's avatar
romanb committed
121
    }
122

123
    /**
124
     * Gets the ReflectionProperty for the single identifier field.
125
     *
126
     * @return ReflectionProperty
127
     * @throws BadMethodCallException If the class has a composite identifier.
128
     */
129 130
    public function getSingleIdReflectionProperty()
    {
131
        if ($this->isIdentifierComposite) {
132
            throw new \BadMethodCallException("Class " . $this->name . " has a composite identifier.");
133
        }
134
        return $this->reflFields[$this->identifier[0]];
135
    }
136
    
137
    /**
138
     * Validates & completes the given field mapping.
139
     *
140 141
     * @param array $mapping  The field mapping to validated & complete.
     * @return array  The validated and completed field mapping.
142 143
     * 
     * @throws MappingException
144
     */
145
    protected function _validateAndCompleteFieldMapping(array &$mapping)
146
    {
147 148 149
        parent::_validateAndCompleteFieldMapping($mapping);

        // Store ReflectionProperty of mapped field
150 151 152
        $refProp = $this->reflClass->getProperty($mapping['fieldName']);
        $refProp->setAccessible(true);
        $this->reflFields[$mapping['fieldName']] = $refProp;
153
    }
154

155
    /**
156 157 158 159
     * Extracts the identifier values of an entity of this class.
     * 
     * For composite identifiers, the identifier values are returned as an array
     * with the same order as the field order in {@link identifier}.
160
     *
161
     * @param object $entity
romanb's avatar
romanb committed
162
     * @return array
163
     */
164
    public function getIdentifierValues($entity)
165
    {
166 167 168 169 170
        if ($this->isIdentifierComposite) {
            $id = array();
            foreach ($this->identifier as $idField) {
                $value = $this->reflFields[$idField]->getValue($entity);
                if ($value !== null) {
171
                    $id[$idField] = $value;
172 173 174
                }
            }
            return $id;
175
        } else {
176
            return array($this->identifier[0] => $this->reflFields[$this->identifier[0]]->getValue($entity));
177 178 179
        }
    }

romanb's avatar
romanb committed
180
    /**
181
     * Populates the entity identifier of an entity.
romanb's avatar
romanb committed
182
     *
183 184 185
     * @param object $entity
     * @param mixed $id
     * @todo Rename to assignIdentifier()
romanb's avatar
romanb committed
186
     */
187
    public function setIdentifierValues($entity, $id)
188
    {
189 190 191 192 193 194 195
        if ($this->isIdentifierComposite) {
            foreach ((array)$id as $idField => $idValue) {
                $this->reflFields[$idField]->setValue($entity, $idValue);
            }
        } else {
            $this->reflFields[$this->identifier[0]]->setValue($entity, $id);
        }
196
    }
197

198
    /**
199
     * Sets the specified field to the specified value on the given entity.
200
     *
201 202 203
     * @param object $entity
     * @param string $field
     * @param mixed $value
204
     */
205
    public function setFieldValue($entity, $field, $value)
206
    {
207
        $this->reflFields[$field]->setValue($entity, $value);
208
    }
209

210 211 212 213 214 215 216 217 218 219 220
    /**
     * Gets the specified field's value off the given entity.
     *
     * @param object $entity
     * @param string $field
     */
    public function getFieldValue($entity, $field)
    {
        return $this->reflFields[$field]->getValue($entity);
    }

221
    /**
222
     * Sets the field mapped to the specified column to the specified value on the given entity.
223
     *
224 225 226
     * @param object $entity
     * @param string $field
     * @param mixed $value
227
     */
228
    public function setColumnValue($entity, $column, $value)
229
    {
230
        $this->reflFields[$this->fieldNames[$column]]->setValue($entity, $value);
231
    }
232

233 234 235
    /**
     * Stores the association mapping.
     *
236
     * @param AssociationMapping $assocMapping
237
     */
238
    protected function _storeAssociationMapping(AssociationMapping $assocMapping)
239
    {
240
        parent::_storeAssociationMapping($assocMapping);
241 242

        // Store ReflectionProperty of mapped field
243
        $sourceFieldName = $assocMapping->sourceFieldName;
244
        
245 246 247
	    $refProp = $this->reflClass->getProperty($sourceFieldName);
	    $refProp->setAccessible(true);
	    $this->reflFields[$sourceFieldName] = $refProp;
248
    }
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    /**
     * Gets the (possibly quoted) column name of a mapped field for safe use
     * in an SQL statement.
     * 
     * @param string $field
     * @param AbstractPlatform $platform
     * @return string
     */
    public function getQuotedColumnName($field, $platform)
    {
        return isset($this->fieldMappings[$field]['quoted']) ?
                $platform->quoteIdentifier($this->fieldMappings[$field]['columnName']) :
                $this->fieldMappings[$field]['columnName'];
    }
    
    /**
     * Gets the (possibly quoted) primary table name of this class for safe use
     * in an SQL statement.
     * 
     * @param AbstractPlatform $platform
     * @return string
     */
    public function getQuotedTableName($platform)
    {
        return isset($this->primaryTable['quoted']) ?
                $platform->quoteIdentifier($this->primaryTable['name']) :
                $this->primaryTable['name'];
    }
278

279 280 281 282
    /**
     * Creates a string representation of this instance.
     *
     * @return string The string representation of this instance.
283
     * @todo Construct meaningful string representation.
284
     */
285 286
    public function __toString()
    {
287
        return __CLASS__ . '@' . spl_object_hash($this);
288
    }
289 290 291 292 293 294 295 296 297 298 299 300 301
    
    /**
     * Determines which fields get serialized.
     * 
     * Parts that are NOT serialized because they can not be properly unserialized:
     *      - reflClass (ReflectionClass)
     *      - reflFields (ReflectionProperty array)
     * 
     * @return array The names of all the fields that should be serialized.
     */
    public function __sleep()
    {
        return array(
302
            'associationMappings', // unserialization "bottleneck" with many associations
303
            'changeTrackingPolicy',
romanb's avatar
romanb committed
304
            'columnNames', //TODO: Not really needed. Can use fieldMappings[$fieldName]['columnName']
305 306 307
            'customRepositoryClassName',
            'discriminatorColumn',
            'discriminatorValue',
romanb's avatar
romanb committed
308
            'discriminatorMap',
309
            'fieldMappings',
romanb's avatar
romanb committed
310
            'fieldNames', //TODO: Not all of this stuff needs to be serialized. Only type, columnName and fieldName.
311 312
            'generatorType',
            'identifier',
romanb's avatar
romanb committed
313
            'idGenerator', //TODO: Does not really need to be serialized. Could be moved to runtime.
314 315
            'inheritanceType',
            'inheritedAssociationFields',
316
            'inverseMappings', //TODO: Remove! DDC-193
317 318 319 320 321 322 323 324 325 326 327 328 329 330
            'isIdentifierComposite',
            'isMappedSuperclass',
            'isVersioned',
            'lifecycleCallbacks',
            'name',
            'parentClasses',
            'primaryTable',
            'rootEntityName',
            'subClasses',
            'versionField'
        );
    }
    
    /**
331
     * Restores some state that can not be serialized/unserialized.
332 333 334 335 336 337 338
     * 
     * @return void
     */
    public function __wakeup()
    {
        // Restore ReflectionClass and properties
        $this->reflClass = new \ReflectionClass($this->name);
339
        
340
        foreach ($this->fieldMappings as $field => $mapping) {
341 342 343 344 345
	        if (isset($mapping['inherited'])) {
	            $reflField = new \ReflectionProperty($mapping['inherited'], $field);
	        } else {
	            $reflField = $this->reflClass->getProperty($field);
	        }
346
	            
347 348
            $reflField->setAccessible(true);
            $this->reflFields[$field] = $reflField;
349
        }
350
        
351
        foreach ($this->associationMappings as $field => $mapping) {
352 353 354 355 356
            if (isset($this->inheritedAssociationFields[$field])) {
                $reflField = new \ReflectionProperty($this->inheritedAssociationFields[$field], $field);
            } else {
                $reflField = $this->reflClass->getProperty($field);
            }
357

358 359
            $reflField->setAccessible(true);
            $this->reflFields[$field] = $reflField;
360 361
        }
    }
362
    
363 364 365 366 367 368 369 370 371 372 373 374
    /**
     * Creates a new instance of the mapped class, without invoking the constructor.
     * 
     * @return object
     */
    public function newInstance()
    {
        if ($this->_prototype === null) {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }
        return clone $this->_prototype;
    }
375
}