本文整理汇总了PHP中Doctrine\Common\Persistence\ObjectManager::getClassMetadata方法的典型用法代码示例。如果您正苦于以下问题:PHP ObjectManager::getClassMetadata方法的具体用法?PHP ObjectManager::getClassMetadata怎么用?PHP ObjectManager::getClassMetadata使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\Common\Persistence\ObjectManager
的用法示例。
在下文中一共展示了ObjectManager::getClassMetadata方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getFormSpecification
/**
* Overrides the base getFormSpecification() to additionally iterate through each
* field/association in the metadata and trigger the associated event.
*
* This allows building of a form from metadata instead of requiring annotations.
* Annotations are still allowed through the ElementAnnotationsListener.
*
* {@inheritDoc}
*/
public function getFormSpecification($entity)
{
$formSpec = parent::getFormSpecification($entity);
$metadata = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity);
$inputFilter = $formSpec['input_filter'];
foreach ($formSpec['elements'] as $key => $elementSpec) {
$name = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null;
if (!$name) {
continue;
}
if (!isset($inputFilter[$name])) {
$inputFilter[$name] = new \ArrayObject();
}
$params = array('metadata' => $metadata, 'name' => $name, 'elementSpec' => $elementSpec, 'inputSpec' => $inputFilter[$name]);
if ($this->checkForExcludeElementFromMetadata($metadata, $name)) {
$elementSpec = $formSpec['elements'];
unset($elementSpec[$key]);
$formSpec['elements'] = $elementSpec;
if (isset($inputFilter[$name])) {
unset($inputFilter[$name]);
}
$formSpec['input_filter'] = $inputFilter;
continue;
}
if ($metadata->hasField($name)) {
$this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params);
} elseif ($metadata->hasAssociation($name)) {
$this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params);
}
}
return $formSpec;
}
示例2: getFormSpecification
/**
* Overrides the base getFormSpecification() to additionally iterate through each
* field/association in the metadata and trigger the associated event.
*
* This allows building of a form from metadata instead of requiring annotations.
* Annotations are still allowed through the ElementAnnotationsListener.
*
* {@inheritDoc}
*/
public function getFormSpecification($entity)
{
$formSpec = parent::getFormSpecification($entity);
$metadata = $this->objectManager->getClassMetadata(is_object($entity) ? get_class($entity) : $entity);
$inputFilter = $formSpec['input_filter'];
$formElements = array('DoctrineModule\\Form\\Element\\ObjectSelect', 'DoctrineModule\\Form\\Element\\ObjectMultiCheckbox', 'DoctrineModule\\Form\\Element\\ObjectRadio');
foreach ($formSpec['elements'] as $key => $elementSpec) {
$name = isset($elementSpec['spec']['name']) ? $elementSpec['spec']['name'] : null;
$isFormElement = isset($elementSpec['spec']['type']) && in_array($elementSpec['spec']['type'], $formElements);
if (!$name) {
continue;
}
if (!isset($inputFilter[$name])) {
$inputFilter[$name] = new \ArrayObject();
}
$params = array('metadata' => $metadata, 'name' => $name, 'elementSpec' => $elementSpec, 'inputSpec' => $inputFilter[$name]);
if ($this->checkForExcludeElementFromMetadata($metadata, $name)) {
$elementSpec = $formSpec['elements'];
unset($elementSpec[$key]);
$formSpec['elements'] = $elementSpec;
if (isset($inputFilter[$name])) {
unset($inputFilter[$name]);
}
$formSpec['input_filter'] = $inputFilter;
continue;
}
if ($metadata->hasField($name) || !$metadata->hasAssociation($name) && $isFormElement) {
$this->getEventManager()->trigger(static::EVENT_CONFIGURE_FIELD, $this, $params);
} elseif ($metadata->hasAssociation($name)) {
$this->getEventManager()->trigger(static::EVENT_CONFIGURE_ASSOCIATION, $this, $params);
}
}
$formSpec['options'] = array('prefer_form_input_filter' => true);
return $formSpec;
}
示例3: setUp
/**
* Test setup
*/
protected function setUp()
{
// setup entity aliases
$this->em = DoctrineTestHelper::createTestEntityManager();
$entityManagerNamespaces = $this->em->getConfiguration()->getEntityNamespaces();
$entityManagerNamespaces['WebtownPhpBannerBundle'] = 'WebtownPhp\\BannerBundle\\Entity';
$this->em->getConfiguration()->setEntityNamespaces($entityManagerNamespaces);
// setup schema
$schemaTool = new SchemaTool($this->em);
$classes = [];
foreach ($this->getEntities() as $entityClass) {
$classes[] = $this->em->getClassMetadata($entityClass);
}
try {
$schemaTool->dropSchema($classes);
} catch (\Exception $e) {
}
try {
$schemaTool->createSchema($classes);
} catch (\Exception $e) {
}
$registry = \Mockery::mock('Doctrine\\Bundle\\DoctrineBundle\\Registry');
$registry->shouldReceive('getManager')->andReturn($this->em);
$this->bm = new ORMManager($registry, new EventDispatcher());
}
示例4: doGetIdentificator
/**
* {@inheritDoc}
*/
protected function doGetIdentificator($model)
{
$modelMetadata = $this->objectManager->getClassMetadata(get_class($model));
$id = $modelMetadata->getIdentifierValues($model);
if (count($id) > 1) {
throw new \LogicException('Storage not support composite primary ids');
}
return new Identificator(array_shift($id), $model);
}
示例5: __construct
public function __construct(ObjectManager $om, $accountSignatureClass, $accountOwnerSignatureClass)
{
$this->om = $om;
$this->documentSignatureRepository = $om->getRepository($accountSignatureClass);
$this->ownerSignatureRepository = $om->getRepository($accountOwnerSignatureClass);
$accountSignatureMetadata = $om->getClassMetadata($accountSignatureClass);
$this->accountSignatureClass = $accountSignatureMetadata->getName();
$ownerSignatureMetadata = $om->getClassMetadata($accountOwnerSignatureClass);
$this->ownerSignatureClass = $ownerSignatureMetadata->getName();
}
示例6: getOptions
/**
* {@inheritDoc}
*/
public function getOptions($object)
{
$meta = $this->om->getClassMetadata(get_class($object));
if (!isset($this->options[$meta->name])) {
$config = $this->sluggable->getConfiguration($this->om, $meta->name);
$options = $config['handlers'][get_called_class()];
$default = array('separator' => '/');
$this->options[$meta->name] = array_merge($default, $options);
}
return $this->options[$meta->name];
}
示例7: createResult
/**
* @param $command
* @param $entity
* @param bool $addExtractedEntity
*
* @return Result
*/
protected function createResult($command, $entity, $addExtractedEntity = true)
{
$meta = $this->objectManager->getClassMetadata($this->className);
$identifiers = $meta->getIdentifierValues($entity);
$result = new Result($command, current($identifiers));
if (!$addExtractedEntity) {
return $result;
}
$data = $this->hydrator->extract($entity);
$result->addParams(['item' => $data]);
return $result;
}
示例8: reverseTransform
/**
* Transforms an array including an identifier to an object.
*
* @param array $idObject
*
* @throws TransformationFailedException if object is not found.
*
* @return object|null
*/
public function reverseTransform($idObject)
{
if (!is_array($idObject)) {
return;
}
$identifier = current(array_values($this->om->getClassMetadata($this->entityName)->getIdentifier()));
$id = $idObject[$identifier];
$object = $this->om->getRepository($this->entityName)->findOneBy([$identifier => $id]);
if (null === $object) {
throw new TransformationFailedException(sprintf('An object with identifier key "%s" and value "%s" does not exist!', $identifier, $id));
}
return $object;
}
示例9: hydrate
/**
* Hydrate $object with the provided $data.
*
* @param array $data
* @param object $object
* @throws \Exception
* @return object
*/
public function hydrate(array $data, $object)
{
$this->metadata = $this->objectManager->getClassMetadata(get_class($object));
foreach ($data as $field => &$value) {
if ($this->metadata->hasAssociation($field)) {
$target = $this->metadata->getAssociationTargetClass($field);
if ($this->metadata->isSingleValuedAssociation($field)) {
$value = $this->toOne($value, $target);
} elseif ($this->metadata->isCollectionValuedAssociation($field)) {
$value = $this->toMany($value, $target);
}
}
}
return $this->hydrator->hydrate($data, $object);
}
示例10: transform
/**
* (non-PHPdoc)
* @see \Symfony\Component\Form\DataTransformerInterface::transform()
*/
public function transform($entity)
{
if (null === $entity || '' === $entity) {
return null;
}
if (!is_object($entity)) {
throw new UnexpectedTypeException($entity, 'object');
}
if ($entity instanceof Proxy && !$entity->__isInitialized()) {
$entity->__load();
}
$meta = $this->om->getClassMetadata(get_class($entity));
$id = $meta->getSingleIdReflectionProperty()->getValue($entity);
return $id;
}
示例11: getObjects
/**
* Returns objects extracted from simple search
*
* @param User $user
* @param string $entityClass
* @param string $searchString
* @param int $offset
* @param int $maxResults
*
* @return array
*/
protected function getObjects(User $user, $entityClass, $searchString, $offset, $maxResults)
{
$objects = [];
if (!$this->configManager->hasConfig($entityClass)) {
return $objects;
}
$classNames = $this->shareScopeProvider->getClassNamesBySharingScopeConfig($entityClass);
if (!$classNames) {
return $objects;
}
$tables = [];
foreach ($classNames as $className) {
$metadata = $this->em->getClassMetadata($className);
$tables[] = $metadata->getTableName();
}
$searchResults = $this->indexer->simpleSearch($searchString, $offset, $maxResults, $tables);
list($userIds, $buIds, $orgIds) = $this->getIdsByClass($searchResults, $user);
if ($orgIds) {
$organizations = $this->em->getRepository('OroOrganizationBundle:Organization')->getEnabledOrganizations($orgIds);
$objects = array_merge($objects, $organizations);
}
if ($buIds) {
$businessUnits = $this->em->getRepository('OroOrganizationBundle:BusinessUnit')->getBusinessUnits($buIds);
$objects = array_merge($objects, $businessUnits);
}
if ($userIds) {
$users = $this->em->getRepository('OroUserBundle:User')->findUsersByIds($userIds);
$objects = array_merge($objects, $users);
}
return $objects;
}
示例12: load
public function load(ObjectManager $manager)
{
$data = (include __DIR__ . '/../fixtures/user/users.php');
$encoder = $this->container->get('security.password_encoder');
$userManager = $this->container->get('medievistes.user_manager');
$connection = $manager->getConnection();
if ($connection->getDatabasePlatform()->getName() === 'mysql') {
$connection->exec("ALTER TABLE {$manager->getClassMetadata(User::class)->getTableName()} AUTO_INCREMENT = 1;");
}
foreach ($data as $userData) {
$class = $userManager->getUserClass($userData['type']);
$user = (new $class())->setId((int) $userData['id'])->setUsername($userData['username'])->setEmail($userData['email'])->setPlainPassword($userData['plain_password'])->setSalt(md5(uniqid(null, true)))->enable((bool) $userData['is_active'])->setCreatedAt(new \DateTime($userData['created_at']))->setUpdatedAt(new \DateTime($userData['updated_at']));
if (!empty($userData['activation_link_id'])) {
$user->setActivationLink($this->getReference("activation-link-{$userData['activation_link_id']}"));
}
foreach ($userData['roles'] as $role) {
$user->addRole($role);
}
foreach ($userData['troops'] as $troop) {
$association = (new Association())->setUser($user)->setTroop($this->getReference("troop-{$troop['troop_id']}"))->setRole($this->getReference("troop-role-{$troop['role_id']}"));
$user->addTroopAssociation($association);
$manager->persist($association);
}
$password = $encoder->encodePassword($user, $userData['plain_password']);
$user->setPassword($password);
$manager->persist($user);
$manager->flush();
$this->addReference("user-{$user->getId()}", $user);
}
$manager->clear($class);
$manager->clear(Association::class);
}
示例13: truncate
/**
*
* @param ObjectManager|EntityManager $manager
* @param
* $class
* @return bool
*/
public static function truncate(ObjectManager $manager, $class)
{
/**
* @var $connection \Doctrine\DBAL\Connection
*/
$connection = $manager->getConnection();
/**
* @var $cmd ClassMetadata
*/
$cmd = $manager->getClassMetadata($class);
$connection->beginTransaction();
try {
if ($connection->getDatabasePlatform()->getName() !== 'sqlite') {
$connection->query('SET FOREIGN_KEY_CHECKS=0');
$connection->executeUpdate($connection->getDatabasePlatform()->getTruncateTableSql($cmd->getTableName()));
$connection->query('SET FOREIGN_KEY_CHECKS=1');
} else {
$connection->executeUpdate($connection->getDatabasePlatform()->getTruncateTableSql($cmd->getTableName()));
}
$connection->commit();
return true;
} catch (\Exception $e) {
$connection->rollback();
return false;
}
}
示例14: buildAssociationValue
/**
* Builds the association value.
*
* @param ClassMetadata $metadata
* @param string $propertyPath
* @param string $value
*
* @return array|object
* @throws \Exception
*/
private function buildAssociationValue(ClassMetadata $metadata, $propertyPath, $value)
{
$childMetadata = $this->manager->getClassMetadata($metadata->getAssociationTargetClass($propertyPath));
// Single association
if ($metadata->isSingleValuedAssociation($propertyPath)) {
if (is_string($value) && '#' === substr($value, 0, 1)) {
return $this->getReference(substr($value, 1));
} elseif (is_array($value)) {
return $this->buildEntity($childMetadata, $value);
}
throw new \Exception("Unexpected value for single association '{$propertyPath}'.");
// Collection association
} elseif ($metadata->isCollectionValuedAssociation($propertyPath)) {
if (!is_array($value)) {
throw new \Exception('Expected array.');
}
$builtValue = [];
foreach ($value as $childData) {
if (is_string($childData) && '#' === substr($childData, 0, 1)) {
array_push($builtValue, $this->getReference(substr($childData, 1)));
} elseif (is_array($value)) {
array_push($builtValue, $this->buildEntity($childMetadata, $childData));
} else {
throw new \Exception("Unexpected value for association '{$propertyPath}'.");
}
}
return $builtValue;
}
throw new \Exception("Unexpected association path '{$propertyPath}'.");
}
示例15: getClassMetadata
/**
* @param string $className
* @return ClassMetadata
*/
private function getClassMetadata($className)
{
if (null === $this->objectManager) {
return null;
}
return $this->objectManager->getClassMetadata($className);
}