使用Doctrine2的CodeIgniter到Symfony2

时间:2015-04-27 07:49:32

标签: php codeigniter symfony doctrine-orm

我真的很难将此查询转换为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();
}
你可以帮我解决这个问题吗? 有什么建议?感谢

2 个答案:

答案 0 :(得分:2)

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

注意:

由于您没有提供任何实体映射,因此这完全出乎意料。您很可能会更改此查询中的属性,但至少应该为您提供所需的开始。

如果你不明白的话,不要犹豫。

答案 1 :(得分:1)

我总是发现直接写dql更容易。试图将查询构建器用于复杂的东西让我疯狂。这显然要求您在实体中映射正确的关系,无论是注释还是使用orm文件。

显然很难测试我下面的内容,所以你可能需要调试一下。

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