123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
-
-
- namespace Doctrine\ORM\Mapping;
-
- use ReflectionClass, ReflectionProperty;
-
-
- class ClassMetadata extends ClassMetadataInfo
- {
-
-
- public $reflFields = array();
-
-
-
- private $_prototype;
-
-
-
- public function __construct($entityName)
- {
- $this->reflClass = new ReflectionClass($entityName);
- $this->namespace = $this->reflClass->getNamespaceName();
- $this->table['name'] = $this->reflClass->getShortName();
- parent::__construct($this->reflClass->getName());
- }
-
-
-
- public function getReflectionProperties()
- {
- return $this->reflFields;
- }
-
-
-
- public function getReflectionProperty($name)
- {
- return $this->reflFields[$name];
- }
-
-
-
- public function getSingleIdReflectionProperty()
- {
- if ($this->isIdentifierComposite) {
- throw new \BadMethodCallException("Class " . $this->name . " has a composite identifier.");
- }
- return $this->reflFields[$this->identifier[0]];
- }
-
-
-
- protected function _validateAndCompleteFieldMapping(array &$mapping)
- {
- parent::_validateAndCompleteFieldMapping($mapping);
-
-
- $refProp = $this->reflClass->getProperty($mapping['fieldName']);
- $refProp->setAccessible(true);
- $this->reflFields[$mapping['fieldName']] = $refProp;
- }
-
-
-
- public function getIdentifierValues($entity)
- {
- if ($this->isIdentifierComposite) {
- $id = array();
- foreach ($this->identifier as $idField) {
- $value = $this->reflFields[$idField]->getValue($entity);
- if ($value !== null) {
- $id[$idField] = $value;
- }
- }
- return $id;
- } else {
- $value = $this->reflFields[$this->identifier[0]]->getValue($entity);
- if ($value !== null) {
- return array($this->identifier[0] => $value);
- }
- return array();
- }
- }
-
-
-
- public function setIdentifierValues($entity, array $id)
- {
- foreach ($id as $idField => $idValue) {
- $this->reflFields[$idField]->setValue($entity, $idValue);
- }
- }
-
-
-
- public function setFieldValue($entity, $field, $value)
- {
- $this->reflFields[$field]->setValue($entity, $value);
- }
-
-
-
- public function getFieldValue($entity, $field)
- {
- return $this->reflFields[$field]->getValue($entity);
- }
-
-
-
- protected function _storeAssociationMapping(array $assocMapping)
- {
- parent::_storeAssociationMapping($assocMapping);
-
-
- $sourceFieldName = $assocMapping['fieldName'];
-
- $refProp = $this->reflClass->getProperty($sourceFieldName);
- $refProp->setAccessible(true);
- $this->reflFields[$sourceFieldName] = $refProp;
- }
-
-
-
- public function __toString()
- {
- return __CLASS__ . '@' . spl_object_hash($this);
- }
-
-
-
- public function __sleep()
- {
-
- $serialized = array(
- 'associationMappings',
- 'columnNames',
- 'fieldMappings',
- 'fieldNames',
- 'identifier',
- 'isIdentifierComposite',
- 'name',
- 'namespace',
- 'table',
- 'rootEntityName',
- 'idGenerator',
- );
-
-
- if ($this->changeTrackingPolicy != self::CHANGETRACKING_DEFERRED_IMPLICIT) {
- $serialized[] = 'changeTrackingPolicy';
- }
-
- if ($this->customRepositoryClassName) {
- $serialized[] = 'customRepositoryClassName';
- }
-
- if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE) {
- $serialized[] = 'inheritanceType';
- $serialized[] = 'discriminatorColumn';
- $serialized[] = 'discriminatorValue';
- $serialized[] = 'discriminatorMap';
- $serialized[] = 'parentClasses';
- $serialized[] = 'subClasses';
- }
-
- if ($this->generatorType != self::GENERATOR_TYPE_NONE) {
- $serialized[] = 'generatorType';
- if ($this->generatorType == self::GENERATOR_TYPE_SEQUENCE) {
- $serialized[] = 'sequenceGeneratorDefinition';
- }
- }
-
- if ($this->isMappedSuperclass) {
- $serialized[] = 'isMappedSuperclass';
- }
-
- if ($this->containsForeignIdentifier) {
- $serialized[] = 'containsForeignIdentifier';
- }
-
- if ($this->isVersioned) {
- $serialized[] = 'isVersioned';
- $serialized[] = 'versionField';
- }
-
- if ($this->lifecycleCallbacks) {
- $serialized[] = 'lifecycleCallbacks';
- }
-
- if ($this->namedQueries) {
- $serialized[] = 'namedQueries';
- }
-
- if ($this->isReadOnly) {
- $serialized[] = 'isReadOnly';
- }
-
- return $serialized;
- }
-
-
-
- public function __wakeup()
- {
-
- $this->reflClass = new ReflectionClass($this->name);
-
- foreach ($this->fieldMappings as $field => $mapping) {
- if (isset($mapping['declared'])) {
- $reflField = new ReflectionProperty($mapping['declared'], $field);
- } else {
- $reflField = $this->reflClass->getProperty($field);
- }
- $reflField->setAccessible(true);
- $this->reflFields[$field] = $reflField;
- }
-
- foreach ($this->associationMappings as $field => $mapping) {
- if (isset($mapping['declared'])) {
- $reflField = new ReflectionProperty($mapping['declared'], $field);
- } else {
- $reflField = $this->reflClass->getProperty($field);
- }
-
- $reflField->setAccessible(true);
- $this->reflFields[$field] = $reflField;
- }
- }
-
-
-
- public function newInstance()
- {
- if ($this->_prototype === null) {
- $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
- }
- return clone $this->_prototype;
- }
- }
|