简体   繁体   English

使用Doctrine2的CodeIgniter到Symfony2

[英]CodeIgniter to Symfony2 with Doctrine2

I'm really having a hard time converting this query to Doctrine: 我真的很难将这个查询转换为Doctrine:

public function get_order($id)
{
    $this->db->select('*');
    $this->db->from('tbl_orderline');
    $this->db->join('tbl_order', 'tbl_order.orderNo = tbl_orderline.orderNo');
    $this->db->join('tbl_customer', 'tbl_customer.customerNo = tbl_order.customerNo');
    $this->db->join('tbl_product', 'tbl_product.productNo = tbl_orderline.productNo');
    $this->db->where('tbl_order.orderNo', $id);

    $query = $this->db->get();
    return $query->result_array();
}

Could you please help me with this? 你能帮帮我吗? Any suggestions? 有什么建议? Thanks 谢谢

// if you're currently in custom Repository class then:
$qb = $this->createQueryBuilder('ol');

// if you're in a controller, then should be:
$qb = $em->getRepository('AppBundle:OrderLine')->createQueryBuilder('ol'); // Or whatever your bundle name is.

// query alias legend:

// ol - order line
// o - order
// c - customer
// p - product

// Your query builder should look something like this:

$qb
    ->addSelect('o, c, p')
    ->leftJoin('ol.order', 'o') // this is your relation with [order]
    ->leftJoin('o.customer', 'c') // this is your relation with [customer] from [order]
    ->leftJoin('ol.product', 'p') // this is your relation with [product] from [order line]
    ->where($qb->expr()->eq('ol.id', ':orderLineId')
    ->setParameter('orderLineId', $id)
    ->getQuery()
    ->getOneOrNullResult();

Note: 注意:

Since you did not provide any entity mappings, this is completely out of the blue. 由于您没有提供任何实体映射,因此这完全是出乎意料的。 You are most likely going to change the properties in this query, but at the very least, it should give you the start you need. 您很可能会更改此查询中的属性,但至少应该为您提供所需的开始。

Don't hesitate to ask, if you don't understand something. 如果你不明白的话,不要犹豫。

Ive always found it easier to write in straight dql . 我总是发现直接用dql编写更容易。 Trying to use the querybuilder for complex stuff drives me nuts. 试图将查询构建器用于复杂的东西让我疯狂。 This obviously requires you to have the correct relationships mapped in your entities, either annotated or with an orm file. 这显然要求您在实体中映射正确的关系,无论是注释还是与orm文件。

Obviously its hard to test what Ive put below, so you may need to debug a little. 显然很难测试我下面的内容,所以你可能需要调试一下。

$query = $this->getEntityManager()->createQuery(
'select orderline, order, customer, product
from BundleName:tlb_orderline orderline
join orderline.orderNo order
join order.customerNo customer
join orderline.productNo product
where order.orderNo = :id');

$query->setParameter('id' => $id);

return $query->getResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM