本文整理汇总了PHP中Doctrine\ORM\Mapping\ClassMetadata类的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata类的具体用法?PHP ClassMetadata怎么用?PHP ClassMetadata使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ClassMetadata类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testEntityTableNameAndInheritance
/**
* @depends testLoadMapping
* @param ClassMetadata $class
*/
public function testEntityTableNameAndInheritance($class)
{
$this->assertEquals('cms_users', $class->getTableName());
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $class->inheritanceType);
return $class;
}
示例2: __construct
/**
* {@inheritdoc}
*/
public function __construct(EntityManager $em, ClassMetadata $class)
{
if ($class->getReflectionClass()->isSubclassOf('Gedmo\\Translatable\\Entity\\MappedSuperclass\\AbstractPersonalTranslation')) {
throw new \Gedmo\Exception\UnexpectedValueException('This repository is useless for personal translations');
}
parent::__construct($em, $class);
}
示例3: loadMeta
/**
* @param Builder\Metadata $meta
* @param \Doctrine\ORM\Mapping\ClassMetadata $cm
*/
private function loadMeta(Builder\Metadata $meta, \Doctrine\ORM\Mapping\ClassMetadata $cm)
{
$type = null;
if ($cm->hasField($meta->name)) {
$map = $cm->getFieldMapping($meta->name);
$type = $map['type'];
switch ($type) {
case 'smallint':
case 'bigint':
$type = 'integer';
break;
default:
break;
}
if (!isset($map['nullable']) || $map['nullable'] === false && !isset($meta->conditions['required'])) {
$meta->conditions['required'] = true;
}
if (isset($map['length']) && $map['length'] && !isset($meta->conditions['maxLenght'])) {
$meta->conditions['maxLength'] = $map['length'];
}
if ($type === 'decimal' && isset($map['scale'])) {
$type = 'float';
$meta->custom['step'] = pow(10, -$map['scale']);
}
} elseif ($cm->hasAssociation($meta->name)) {
$map = $cm->getAssociationMapping($meta->name);
$type = $map['targetEntity'];
}
if (!$meta->type) {
$meta->type = $type;
}
}
示例4: addDoctrineAssociations
/**
* @param EntityMetadata $entityMetadata
* @param ClassMetadata $classMetadata
*/
protected function addDoctrineAssociations(EntityMetadata $entityMetadata, ClassMetadata $classMetadata)
{
foreach ($classMetadata->getAssociationMappings() as $fieldName => $associationMapping) {
$fieldMetadata = $this->metadataFactory->createFieldMetadata(array('field_name' => $fieldName), $associationMapping);
$entityMetadata->addFieldMetadata($fieldMetadata);
}
}
示例5: addFilterConstraint
/**
* {@inheritdoc}
*/
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
$className = $targetEntity->getName();
if (isset(self::$disabled[$className])) {
return '';
} elseif (array_key_exists($targetEntity->rootEntityName, self::$disabled)) {
return '';
}
$dataKey = $className . '|' . $targetTableAlias;
if (!isset(self::$data[$dataKey])) {
$annotation = $this->getReflectionService()->getClassAnnotation($className, SoftDeletable::class);
if ($annotation !== null) {
$existingProperties = $this->getReflectionService()->getClassPropertyNames($className);
if (!in_array($annotation->deleteProperty, $existingProperties)) {
throw new PropertyNotFoundException("Property '" . $annotation->deleteProperty . "' not found for '" . $className . "'", 1439207432);
}
$conn = $this->getEntityManager()->getConnection();
$platform = $conn->getDatabasePlatform();
$column = $targetEntity->getQuotedColumnName($annotation->deleteProperty, $platform);
$addCondSql = $platform->getIsNullExpression($targetTableAlias . '.' . $column);
if ($annotation->timeAware) {
$addCondSql .= ' OR ' . $targetTableAlias . '.' . $column . ' > ' . $conn->quote(date('Y-m-d H:i:s'));
}
self::$data[$dataKey] = $addCondSql;
} else {
self::$data[$dataKey] = false;
}
}
return self::$data[$dataKey] ? self::$data[$dataKey] : '';
}
示例6: testClassMetadataValidation
/**
* @expectedException Prezent\Doctrine\Translatable\Mapping\MappingException
*/
public function testClassMetadataValidation()
{
$classMetadata = new ClassMetadata('Prezent\\Tests\\Fixture\\BadMappingTranslation');
$classMetadata->initializeReflection(new RuntimeReflectionService());
$eventArgs = new LoadClassMetadataEventArgs($classMetadata, $this->getEntityManager());
$this->getTranslatableListener()->loadClassMetadata($eventArgs);
}
示例7: let
function let(EntityManager $em, Connection $connection)
{
$classMetadata = new ClassMetadata('Acme\\Bundle\\AppBundle\\Entity\\Color');
$classMetadata->mapField(['fieldName' => 'sortOrder', 'type' => 'integer']);
$em->getConnection()->willReturn($connection);
$this->beConstructedWith($em, $classMetadata);
}
示例8: prepareStubData
/**
* Prepare stub data and mocks
*
* @return array
*/
protected function prepareStubData()
{
// create product stubs
$productEntities = array();
for ($i = 1; $i <= 5; $i++) {
$indexerItem = new Item($this->entityManager, Product::getEntityName(), $i);
$entity = new Product($i);
$productEntities[$i] = $entity;
$this->productStubs[$i] = array('indexer_item' => $indexerItem, 'entity' => $entity);
}
$productMetadata = new ClassMetadata(Product::getEntityName());
$productMetadata->setIdentifier(array('id'));
$reflectionProperty = new \ReflectionProperty('Oro\\Bundle\\SearchBundle\\Tests\\Unit\\Formatter\\Stub\\Product', 'id');
$reflectionProperty->setAccessible(true);
$productMetadata->reflFields['id'] = $reflectionProperty;
// create category stubs
$categoryEntities = array();
for ($i = 1; $i <= 3; $i++) {
$indexerItem = new Item($this->entityManager, Category::getEntityName(), $i);
$entity = new Category($i);
$categoryEntities[$i] = $entity;
$this->categoryStubs[$i] = array('indexer_item' => $indexerItem, 'entity' => $entity);
}
$categoryMetadata = new ClassMetadata(Category::getEntityName());
$categoryMetadata->setIdentifier(array('id'));
$reflectionProperty = new \ReflectionProperty('Oro\\Bundle\\SearchBundle\\Tests\\Unit\\Formatter\\Stub\\Category', 'id');
$reflectionProperty->setAccessible(true);
$categoryMetadata->reflFields['id'] = $reflectionProperty;
// create metadata factory for stubs
$this->stubMetadata = new ClassMetadataFactory($this->entityManager);
$this->stubMetadata->setMetadataFor(Product::getEntityName(), $productMetadata);
$this->stubMetadata->setMetadataFor(Category::getEntityName(), $categoryMetadata);
$this->entityManager->expects($this->any())->method('getMetadataFactory')->will($this->returnValue($this->stubMetadata));
return array(Product::getEntityName() => $productEntities, Category::getEntityName() => $categoryEntities);
}
示例9: loadMetadata
/**
* Metadata definition for static_php metadata driver.
* @param ClassMetadata $metadata
* @return void
*/
public static function loadMetadata(ClassMetadata $metadata)
{
$metadata->setPrimaryTable(['name' => 'password_resets']);
$metadata->mapField(['id' => true, 'fieldName' => 'email', 'type' => 'string']);
$metadata->mapField(['fieldName' => 'token', 'type' => 'string']);
$metadata->mapField(['columnName' => 'created_at', 'fieldName' => 'createdAt', 'type' => 'datetime', 'nullable' => false]);
}
示例10: prepareMocks
/**
* @return array
*/
protected function prepareMocks()
{
$configuration = new Configuration();
$configuration->addEntityNamespace('Stub', 'Oro\\Bundle\\BatchBundle\\Tests\\Unit\\ORM\\Query\\Stub');
$classMetadata = new ClassMetadata('Entity');
$classMetadata->mapField(['fieldName' => 'a', 'columnName' => 'a']);
$classMetadata->mapField(['fieldName' => 'b', 'columnName' => 'b']);
$classMetadata->setIdentifier(['a']);
$platform = $this->getMockBuilder('Doctrine\\DBAL\\Platforms\\AbstractPlatform')->setMethods([])->disableOriginalConstructor()->getMockForAbstractClass();
$statement = $this->getMockBuilder('Doctrine\\DBAL\\Statement')->setMethods(['fetch', 'fetchColumn', 'closeCursor'])->disableOriginalConstructor()->getMock();
$driverConnection = $this->getMockBuilder('Doctrine\\DBAL\\Driver\\Connection')->setMethods(['query'])->disableOriginalConstructor()->getMockForAbstractClass();
$driverConnection->expects($this->any())->method('query')->will($this->returnValue($statement));
$driver = $this->getMockBuilder('Doctrine\\DBAL\\Driver')->setMethods(['connect', 'getDatabasePlatform'])->disableOriginalConstructor()->getMockForAbstractClass();
$driver->expects($this->any())->method('connect')->will($this->returnValue($driverConnection));
$driver->expects($this->any())->method('getDatabasePlatform')->will($this->returnValue($platform));
$connection = $this->getMockBuilder('Doctrine\\DBAL\\Connection')->setMethods(['getDatabasePlatform', 'executeQuery'])->setConstructorArgs([[], $driver])->getMock();
$connection->expects($this->any())->method('getDatabasePlatform')->will($this->returnValue($platform));
/** @var UnitOfWork $unitOfWork */
$unitOfWork = $this->getMockBuilder('UnitOfWork')->setMethods(['getEntityPersister'])->disableOriginalConstructor()->getMock();
$entityManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->setMethods(['getConfiguration', 'getClassMetadata', 'getConnection', 'getUnitOfWork'])->disableOriginalConstructor()->getMock();
$entityManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
$entityManager->expects($this->any())->method('getClassMetadata')->will($this->returnValue($classMetadata));
$entityManager->expects($this->any())->method('getConnection')->will($this->returnValue($connection));
$entityManager->expects($this->any())->method('getUnitOfWork')->will($this->returnValue($unitOfWork));
return [$entityManager, $connection, $statement];
}
示例11: processDiscriminatorValues
/**
* Collecting discriminator map entries from child classes for entities with inheritance not equals NONE
*
* @param ClassMetadata $class
* @param EntityManager $em
*
* @throws MappingException
*/
protected function processDiscriminatorValues(ClassMetadata $class, EntityManager $em)
{
if (!$class->isInheritanceTypeNone()) {
if ($class->isRootEntity()) {
$allClasses = $em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
$FQCN = $class->getName();
$map = $class->discriminatorMap ?: [];
$duplicates = [];
foreach ($allClasses as $subClassCandidate) {
if (is_subclass_of($subClassCandidate, $FQCN) && !in_array($subClassCandidate, $map, true)) {
$value = $this->getDiscriminatorValue($em->getMetadataFactory(), $subClassCandidate);
if (null !== $value) {
if (isset($map[$value])) {
$duplicates[] = $value;
}
$map[$value] = $subClassCandidate;
}
}
}
if ($duplicates) {
throw MappingException::duplicateDiscriminatorEntry($class->getName(), $duplicates, $map);
}
$class->setDiscriminatorMap($map);
$this->collectedMaps = array_merge($this->collectedMaps, array_fill_keys(array_values($map), $map));
} elseif (isset($this->collectedMaps[$class->name]) && $class->discriminatorMap !== $this->collectedMaps[$class->name]) {
$class->setDiscriminatorMap($this->collectedMaps[$class->name]);
}
}
}
示例12: logEntityChange
/**
* @param $action
* @param \Doctrine\ORM\Mapping\ClassMetadata $meta
* @param $entity
*/
private function logEntityChange($action, \Doctrine\ORM\Mapping\ClassMetadata $meta, $entity)
{
$userToken = $this->container->get('security.context')->getToken();
if (null !== $userToken) {
$this->logger->info('Entity "' . $meta->getTableName() . '" with id: ' . $meta->getFieldValue($entity, $meta->getSingleIdentifierFieldName()) . ' ' . $action . ' by: ' . $this->container->get('security.context')->getToken()->getUsername());
}
}
示例13: updateTranslationMetadata
protected function updateTranslationMetadata(ClassMetadata $metadata)
{
if (!$metadata->hasAssociation('translatable')) {
$metadata->mapManyToOne(['fieldName' => 'translatable', 'inversedBy' => 'translations', 'fetch' => $this->translationFetchMode, 'joinColumns' => [['name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE']], 'targetEntity' => substr($metadata->name, 0, -strlen('Translation'))]);
$metadata->table['uniqueConstraints'][] = ['name' => 'unique_translation', 'columns' => ['translatable_id', 'locale']];
}
}
示例14: generate
public function generate($jsonSchema)
{
$schema = json_decode($jsonSchema, true);
if (!isset($schema['type']) && $schema['type'] !== 'object') {
throw new \RuntimeException("Unable to process the schema");
}
if (!isset($schema['title'])) {
throw new \RuntimeException("title property must be defined");
}
// TODO investigate implementation via ClassMetadataBuilder
$className = $schema['title'];
$medatadata = new ClassMetadata($this->getNamespace() . '\\' . $className);
if (isset($schema['properties'])) {
foreach ($schema['properties'] as $name => $definition) {
$type = $definition['type'];
$nullable = isset($schema['required']) ? !in_array($name, $schema['required']) : true;
$medatadata->mapField(['fieldName' => $name, 'type' => $type, 'nullable' => $nullable, 'options' => []]);
}
}
$filename = sprintf("%s/%s/%s.php", $this->getPath(), join('/', explode('\\', $this->getNamespace())), $className);
mkdir(dirname($filename), 0777, true);
$generator = new EntityGenerator();
$generator->setGenerateAnnotations(true);
file_put_contents($filename, $generator->generateEntityClass($medatadata));
}
示例15: testSkipAbstractClassesOnGeneration
/**
* @group DDC-1771
*/
public function testSkipAbstractClassesOnGeneration()
{
$cm = new ClassMetadata(__NAMESPACE__ . '\\AbstractClass');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService());
$this->assertNotNull($cm->reflClass);
$num = $this->proxyFactory->generateProxyClasses(array($cm));
$this->assertEquals(0, $num, "No proxies generated.");
}