【问题标题】:How to implement Complex SQL Query in Doctrine or ORM如何在 Doctrine 或 ORM 中实现复杂的 SQL 查询
【发布时间】:2013-08-28 12:16:12
【问题描述】:

我正在使用 Symphony 2.3.3,并且是 Doctrine 和 ORM 的新手。我阅读了很多关于学说实体管理器、DBAL、DQL 等的内容。我在 SQL 中构建了以下查询,现在想使用上述任何一种方法(即简单的方法)来实现它。

select su.sensor_id, su.user_id
, usr.contact_id, usr.enabled as user_status
, ctct.Email1, ctct.Email2, ctct.active as contact_status, ctct.contacttype_id
, ctctty.`type` as contact_type, ctctty.active as contact_type_status
from sensor_users su, `Users` usr, contacts ctct, contact_types ctctty
where su.user_id = usr.id
and usr.contact_id = ctct.id
and ctct.contacttype_id = ctctty.id
and usr.enabled = 'Y'
and ctct.active   = 'Y'
and ctctty.active = 'Y'
and su.sensor_id = 123;

对如何使用它的早期回复非常感谢。 DBAL,如何从parameters.yml中获取连接。

问候。

【问题讨论】:

    标签: sql symfony orm doctrine dbal


    【解决方案1】:
    $em = $this->getDoctrine()->getEntityManager();
    $query = $em->createQuery(
        'SELECT p
        FROM AcmeStoreBundle:Product p
        WHERE p.price > :price
        ORDER BY p.price ASC'
    )->setParameter('price', '19.99');
    
    $products = $query->getResult();
    

    这是一个来自 symfony 文档的简单示例。你可以用 dql 查询做你想做的事。

    【讨论】:

    • 非常感谢 ybert。我得到了这个例子,但这与我的复杂查询无关。因为我想将上述查询更改为 DQL。任何人都可以发送包含整个连接工作示例的链接。
    【解决方案2】:
    $query = $em->createQuery(
    'SELECT su, usr, ctct, ctctty
    FROM Yoursensor_usersClass su
    JOIN su.YourfieldToJoinUser usr
    JOIN usr.yourFielToJoinContacts ctct
    JOIN ctct.yourFieldToJoinContactType ctctty
    WHERE usr.enabled = :y
    and ctct.active   = :y
    and ctctty.active = :y
    and su.sensor_id = :nb'
    )->setParameter(array(
    'y' => 'Y',
    'nb' => '123'));
    

    应该是这样的。只需替换为管理您的 dbs 表的实体即可。

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 2013-06-09
      相关资源
      最近更新 更多