16

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

4

4 回答 4

25

您可以使用

  1. 命名查询 - 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]

于 2014-05-15T14:15:09.377 回答
2

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

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

于 2012-08-18T07:30:46.537 回答
0

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

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

于 2014-02-25T18:03:06.853 回答
0

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

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

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());
    }
}
于 2016-06-09T01:51:02.527 回答