Doctrine2命名查询 [英] Doctrine2 named queries

查看:127
本文介绍了Doctrine2命名查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't find any documentation about named queries in Doctrine2. Please help. Does Doctrine2 have a named queries feature?

推荐答案

您可以使用


  1. NamedQuery - DQL。例如

  1. NamedQuery - DQL. Example

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();


  • NamedNativeQuery - SQL。更多信息: http://docs.doctrine-project。 org / en / latest / reference / native-sql.html#named-native-query

    在EntityRepository中收集查询,如:

    Collecting a queries in your EntityRepository, like:

    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]

    这篇关于Doctrine2命名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆