public function test_loadMetadataForClass() { $driver = new AnnotationDriver(new AnnotationReader(), __DIR__ . '/../../../Mocks/Entity/'); $cm = new ClassMetadata('Pasinter\\Mocks\\Entity\\Blog\\Post'); $cm->initializeReflection(new RuntimeReflectionService()); $driver->loadMetadataForClass('Pasinter\\Mocks\\Entity\\Blog\\Post', $cm); $this->assertTrue($cm->hasField('title')); $this->assertEquals('string', $cm->getTypeOfField('title')); $this->assertTrue($cm->hasField('fieldWithoutType')); $this->assertEquals('string', $cm->getTypeOfField('fieldWithoutType')); $this->assertTrue($cm->hasField('id')); $this->assertEquals('integer', $cm->getTypeOfField('id')); $this->assertTrue($cm->isIdentifier('id')); $this->assertTrue($cm->hasField('sort')); $this->assertEquals('integer', $cm->getTypeOfField('sort')); $this->assertTrue($cm->hasAssociation('category')); $this->assertEquals('Pasinter\\Mocks\\Entity\\Blog\\Category', $cm->getAssociationTargetClass('category')); $this->assertTrue($cm->hasAssociation('comments')); $this->assertEquals('Pasinter\\Mocks\\Entity\\Blog\\Comment', $cm->getAssociationTargetClass('comments')); $this->assertTrue($cm->hasAssociation('videos')); $this->assertEquals('Pasinter\\Mocks\\Entity\\Video', $cm->getAssociationTargetClass('videos')); $this->assertTrue($cm->hasAssociation('image')); $this->assertEquals('Pasinter\\Mocks\\Entity\\Photo', $cm->getAssociationTargetClass('image')); }
/** * * @param object $entity */ protected function prepareIndexData() { foreach ($this->queuedInserts as $entity) { foreach ($this->cm->getAssociationNames() as $field) { $association = $this->cm->associationMappings[$field]; if (ClassMetadata::REFERENCE_ONE === $association['type']) { $associationObject = $this->cm->getFieldValue($entity, $field); if ($associationObject) { $this->queuedIndexInserts[get_class($entity)][] = ['fields' => [$field => $associationObject->getId()], 'target' => $this->cm->getIdentifierValue($entity)]; } } } } foreach ($this->queuedUpdates as $entity) { foreach ($this->cm->getAssociationNames() as $field) { $association = $this->cm->associationMappings[$field]; if (ClassMetadata::REFERENCE_ONE === $association['type']) { $associationObject = $this->cm->getFieldValue($entity, $field); if ($associationObject) { $this->queuedIndexInserts[get_class($entity)][] = ['fields' => [$field => $associationObject->getId()], 'target' => $this->cm->getIdentifierValue($entity)]; } } } } foreach ($this->queuedDeletes as $entity) { foreach ($this->cm->getAssociationNames() as $field) { $association = $this->cm->associationMappings[$field]; if (ClassMetadata::REFERENCE_ONE === $association['type']) { $associationObject = $this->cm->getFieldValue($entity, $field); if ($associationObject) { $this->queuedIndexDeletes[get_class($entity)][] = ['fields' => [$field => $associationObject->getId()], 'target' => $this->cm->getIdentifierValue($entity)]; } } } } }
/** * @return ClassMetadata */ private function getClassMetadata() { $cm = new ClassMetadata('Pasinter\\Mocks\\Entity\\Blog\\Post'); $cm->initializeReflection(new RuntimeReflectionService()); $cm->identifier = ['id']; $cm->associationMappings = ['comments' => ['type' => ClassMetadata::REFERENCE_MANY, 'target' => 'Pasinter\\Mocks\\Entity\\Blog\\Comment'], 'user' => ['type' => ClassMetadata::REFERENCE_ONE, 'target' => 'Pasinter\\Mocks\\Entity\\User']]; $cm->fieldMappings = ['title' => ['type' => 'string']]; foreach ($cm->reflClass->getProperties() as $reflProp) { $reflProp->setAccessible(true); $cm->reflFields[$reflProp->getName()] = $reflProp; } return $cm; }
/** * * @param array $data * @return string */ private function flattenIdentifier(array $data, ClassMetadata $cm) { $identifierValues = []; $identifierFields = $cm->getIdentifierFieldNames(); sort($identifierFields); foreach ($identifierFields as $identifierField) { $identifierValues[] = isset($data[$identifierField]) ? $data[$identifierField] : null; } return implode('-', $identifierValues); }
/** * * @param object $entity * @param string $oid * @param ClassMetadata $cm * @return integer */ public function getState($entity, $oid, ClassMetadata $cm) { if (isset($this->states[$oid])) { return $this->states[$oid]; } $id = $cm->getIdentifierValue($entity); if (!$id) { return UnitOfWork::STATE_NEW; } if (null !== $this->tryGetById($id, $cm->rootName)) { return UnitOfWork::STATE_DETACHED; } return UnitOfWork::STATE_NEW; }