【问题标题】:Programmatically query Symfony / Doctrine ORM以编程方式查询 Symfony / Doctrine ORM
【发布时间】:2014-07-25 15:53:50
【问题描述】:

我使用 Doctrine 定义的实体模式作为参考来创建 C++/Qt API 来访问此类数据。

有没有办法以编程方式遍历所有字段及其参数,以便只有一个主模式(并且 C++ 样板标头是从中生成的?)

【问题讨论】:

    标签: symfony orm doctrine introspection


    【解决方案1】:

    要获取实体信息,例如属性及其属性,您可以使用 Doctrine Metadata Drivers。你只需要你的实体命名空间,一旦你有了它,你就可以使用 Doctrine 来获取它们的元数据。

    我已经在我的应用程序中为此创建了一个服务(这里发布只是作为一个使用示例):

    namespace Acme\Project\ProjectUserBundle\Service\Mapping;
    
    use Doctrine\ORM\EntityManager;
    use Doctrine\ORM\EntityNotFoundException;
    
    /**
     * Class EntityService
     */
    class EntityService
    {
        private $em;
    
        public function __construct(EntityManager $entityManager)
        {
            $this->em = $entityManager;
        }
    
        /**
         * Return all Doctrine entities namespaces
         *
         * @return array
         */
        public function getAllEntityClasses()
        {
            $doctrineEntities = array();
            $allEntitiesMetadata = $this->em->getMetadataFactory()->getAllMetadata();
    
            foreach ($allEntitiesMetadata as $entityMetadata) {
                $doctrineEntities[] = $entityMetadata->getName();
            }
    
            return $doctrineEntities;
        }
    
        /**
         * Return all Doctrine entities namespaces in a given base namespace
         *
         * @param $namespace
         * @return array
         * @throws \Doctrine\ORM\EntityNotFoundException
         */
        public function getAllEntityClassesInNamespace($namespace)
        {
            $allEntityClasses = $this->getAllEntityClasses();
    
            foreach ($allEntityClasses as $entity) {
                preg_match($namespace, $entity, $haveFound);
                if (!empty($haveFound)) {
                    $entitiesInNamespace[] = $entity;
                }
            }
    
            if (!isset ($entitiesInNamespace)) {
                throw new EntityNotFoundException("No entities found in $namespace namespace");
            }
    
           return $entitiesInNamespace;
        }
    
        /**
         * Receives a entity namespace and return all entity metadata
         *
         * @param $entityNamespace
         * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata
         */
        public function getEntityMetadata($entityNamespace)
        {
            $metadataFactory = $this->em->getMetadataFactory();
            $entityMetadata = $metadataFactory->getMetadataFor($entityNamespace);
    
            //Check out, all entity information here;
            var_dump($entityMetadata);
    
            foreach ($entityMetadata->fieldMappings as $fieldMapping) {
                //Each attribute info;
                var_dump($fieldMapping);
            }
    
            return $entityMetadata;
        }
    }
    

    在你的 services.yml/xml 中注册它,它使用 Doctrine 实体管理器作为依赖。

    services:
        Mapping.EntityService:
            class: Acme\Project\ProjectUserBundle\Service\Mapping\EntityService
            arguments: [ @doctrine.orm.entity_manager ]
    

    不仅仅是在您的控制器上使用该服务(在我的情况下,我使用该服务来获取实体并为用户应用权限(读/写/查看):

    class UserPermissionController extends Controller
    {
    
        public function yourAction()
        {
            $entityService = $this->get('Mapping.EntityService');
    
            $entitiesToApplyPermissions = $entityService->getAllEntityClassesInNamespace('/Acme/');
    
            foreach ($entitiesToApplyPermissions as $entity) {
    
                $entityService->getEntityMetadata($entity);
                //do whatever you want here
            }
        }
    }
    

    请注意,这只是一个使用示例,请查看文档以获取更多信息: http://doctrine-orm.readthedocs.org/en/latest/reference/metadata-drivers.html

    要生成 C++ 标头,您可能可以创建另一个服务来处理,顺便说一句,它可以将此 Mapping.EntityService 用作依赖项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多