如何在查询生成器下重新生成请求

时间:2017-11-17 10:31:36

标签: php mysql sql doctrine-orm

拜托,帮助我!

如何在查询构建器下重新生成此请求。 Doctrine2

    SELECT *
FROM orders AS o
  INNER JOIN states AS s ON o.OrderId = s.orderID

WHERE o.partnerId = 52 AND
      (s.state = 'CANCEL' OR s.state = 'FINISH') AND
      (s.dt BETWEEN '2017-01-01 00:00:00' AND '2017-10-30 00:00:00')

订单

class Order
{
/**
     * @var State[] $states
     * @OneToMany(targetEntity="State", mappedBy="order", cascade={"persist"})
     * @OrderBy({"dt" = "DESC"})
     */
    protected $states;
}

class State
{

    public function __construct($state = null)
    {
        $this->state = $state;
    }

    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Column(type="integer")
     */
    protected $orderId;

    /**
     * @Column(type="datetime")
     */
    protected $dt;

    /**
     * @Column(type="string", columnDefinition="enum('NEW','REGIST','SENTTOSHOP','ARRIVEDTOSHOP','FINISH','CANCEL','RETURNEDTOSTORAGE')")
     */
    protected $state;



    /**
     * @var $order
     * @ManyToOne(targetEntity="Zabberi\Order", inversedBy="states")
     * @JoinColumn(name="orderId", referencedColumnName="OrderId")
     */
    protected $order;
}

很抱歉,如果我不理解你。

有些文字...... |它提供了一组能够以编程方式构建查询的类和方法,还提供了一个流畅的API。这意味着您可以根据需要在一种方法之间进行更改,或者只选择一种方法。>

1 个答案:

答案 0 :(得分:0)

您可以使用查询构建器编写查询

 $qb = $em->getRepository('YourBundle:Order');
 $qb->createQueryBuilder('o')
    ->select('o,s')
    ->join('o.states','s')
    ->where("s.state IN(:states)")
    ->andWhere('s.dt BETWEEN :date1 AND :date2')
   ->setParameter('states', array('CANCEL','FINISH'))
   ->setParameter('date1', '2017-01-01 00:00:00')
   ->setParameter('date2', '2017-10-30 00:00:00')
   ->getQuery()
   ->getResult()
   ;