【问题标题】:Doctrine2 named queriesDoctrine2 命名查询
【发布时间】:2012-08-14 12:38:56
【问题描述】:

我在 Doctrine2 中找不到任何有关命名查询的文档。 请帮忙。 Doctrine2 有命名查询功能吗?

【问题讨论】:

  • 是的,我很早就看到了,但我认为NamedNativeQuery和NamedQuery不一样。你怎么看?
  • 您在此处为您的原始问题提供了一个很好的答案,但这不是一个 SO 答案 - 将其设为答案并接受它可能很有用。
  • 好收获!谢谢!

标签: symfony doctrine-orm


【解决方案1】:

你可以使用

  1. NamedQuery - DQL。示例

    use Doctrine\ORM\Mapping\NamedQuery;
    use Doctrine\ORM\Mapping\NamedQueries;
    
    /**
    * @Entity
    * @Table(name="cms_users")
    * @NamedQueries({
    *     @NamedQuery(name="activated", query="SELECT u FROM __CLASS__ u WHERE u.status = 1")
    * })
    */
    class CmsUser
    {}
    

    然后这样称呼它

    $this->getDoctrine()->getRepository('MyBundle:CmsUser')
        ->createNamedQuery('activated')
        ->getResult();
    
  2. NamedNativeQuery - SQL。更多信息在这里:http://docs.doctrine-project.org/en/latest/reference/native-sql.html#named-native-query

  3. 在您的 EntityRepository 中收集查询,例如:

    namespace Acme\StoreBundle\Repository;
    
    use Doctrine\ORM\EntityRepository;
    
    class ProductRepository extends EntityRepository
    {
        public function findAllOrderedByName()
        {
            return $this->getEntityManager()
                ->createQuery('SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC')
                ->getResult();
        }
    }
    

    更多信息在这里:http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

这里有类似的话题: https://groups.google.com/forum/?fromgroups#!topic/doctrine-user/K-D5ta5tZ3Y[1-25]

【讨论】:

    【解决方案2】:

    也许您会对 EntityRepositories 感兴趣,您可以在其中创建和存储复杂的 Doctrine 查询,并在您想要的项目中调用主题:

    http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

    【讨论】:

    • 谢谢,但没有。我只发现github.com/doctrine/doctrine2/blob/master/tests/Doctrine/Tests/… 如果有人知道我在哪里可以阅读有关 Doctrine2 中的命名查询的信息,请告诉我。 P.S:此处为 Doctrine 1.2 中的命名查询:docs.doctrine-project.org/projects/doctrine1/en/latest/en/…
    • 但是IMO,完全一样!独立的 Doctrine 允许您创建复杂的查询并按名称存储它们以供以后使用。 Symfony2 的 EntityRepository 采用了同样的原则,目的相同:任何比简单的 findBy 更复杂的查询都被编写并存储在一个 Repository 中,在任何地方都可以称为 simplen。
    • 也许,也许 =) 但在 Doctrine 1.2.4 中,我可以创建几个小的命名查询,而不是在 Doctrine_Table 类中定义方法。而且我认为 Doctrine 1.2.4 中的 Doctrine_Table 和 Doctrine 2 中的 Repository 是相同的,但是在 Doctrine 1.2.4 中我仍然可以创建命名查询,我喜欢它。好的,我接受您的回答为已接受。 P.S.:据我所知,EntityRepository 是 Doctrine 的一部分,而不是 symfony
    • 好的,很高兴您找到了 1) 用于命名查询。我试试看,今天学到了一些东西。
    【解决方案3】:

    如果您不想弄乱实体定义,可以在存储库的构造函数中调用注释符号内部使用的函数:

    namespace MyBundle\Repository;
    
    use Doctrine\ORM\EntityRepository;
    
    class CmsUserRepository extends EntityRepository
    {
        public function __construct($em, \Doctrine\ORM\Mapping\ClassMetadata $class)
        {
            $this->getClassMetadata()->addNamedQuery(array(
                'name'  => 'activated',
                'query' => 'SELECT u FROM __CLASS__ u WHERE u.status = 1'
            ));
        }
    }
    

    您还可以将语句的 __CLASS__ 部分替换为您的实体的命名空间和类,例如 MyBundle\Entity\CmsUser

    【讨论】:

      【解决方案4】:

      我在寻找本地命名查询示例时发现了这个问题,上面的答案对我有所帮助,所以我想我会分享如何以同样的方式进行本地命名查询。

      将此添加到您的实体存储库的构造函数中

      public function __construct($em, \Doctrine\ORM\Mapping\ClassMetadata $class)
      {
          parent::__construct($em, $class);
          $this->getClassMetadata()->addNamedNativeQuery(
              array('name' => 'published', 
                    'query' => 'SELECT 
                        t0.id AS id, 
                        t0.tracking_uri AS tracking_uri, 
                        t0.name AS name, 
                        t0.description AS description, 
                        t0.url_name AS url_name,
                        t0.status_type_id as status_type_id
                      FROM 
                        store.store t0 
                      WHERE 
                        t0.status_type_id = 2', 
                     'resultClass' => '__CLASS__',
                     'resultSetMapping' => array('entities' => array('entityClass' => '__CLASS__',
                                                                     'fields'      => array('id'           => 'id',
                                                                                            'tracking_url' => 'tracking_url',
                                                                                            'name'         => 'name',
                                                                                            'description'  => 'description',
                                                                                            'url_name'     => 'url_name',
                                                                                            'status_type'  => 'status_type_id')))));
      }
      

      这是我的测试片段

      public function testNamedNativeQueryPublished()
      {
          $results = $this->em->getRepository('MyBundle:Store')->createNativeNamedQuery('published')->execute();
      
          foreach ($results as $store)
          {
              $this->assertEquals(2, $store->getStatusType());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-19
        • 2017-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多