Php 代码点火器至Symfony2,带Doctrine2
我真的很难将这个查询转换为条令:Php 代码点火器至Symfony2,带Doctrine2,php,codeigniter,symfony,doctrine-orm,Php,Codeigniter,Symfony,Doctrine Orm,我真的很难将这个查询转换为条令: 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_custom
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();
}
你能帮我做这个吗?
有什么建议吗?谢谢我总是觉得用笔直写比较容易。试图用querybuilder处理复杂的东西让我发疯。这显然需要在实体中映射正确的关系,可以是带注释的,也可以是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();
注意:
因为您没有提供任何实体映射,所以这是完全出乎意料的。您很可能会更改此查询中的属性,但至少它会为您提供所需的开始
如果您不了解某些内容,请不要犹豫询问。您的实体是否已映射?映射中存在用于连接的关系?还有其他方法吗?我的意思是,我没有在代码中使用dql,我认为如果我将代码与dql混合使用,效果会不好。但是你的帖子标题说你在使用doctrine2(也就是dql)?使用这两种方法,您现在得到了两个答案。两者都应该做这项工作。
// 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();