Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 您可以使用Doctrine 2 DQL连接子查询吗?_Mysql_Sql_Doctrine Orm_Dql - Fatal编程技术网

Mysql 您可以使用Doctrine 2 DQL连接子查询吗?

Mysql 您可以使用Doctrine 2 DQL连接子查询吗?,mysql,sql,doctrine-orm,dql,Mysql,Sql,Doctrine Orm,Dql,在联接的WITH子句中是否有访问联接实体关系的方法?我试图避免在子查询中使用IN子句 编辑:或者有没有一种方法可以在子查询上连接而不是在中使用 /** * @Entity * @Table(name="order_service_requests") */ class ServiceRequest { const STATUS_REVIEW = 0; const STATUS_PENDING = 1; const STATUS_SCHEDULED = 2; con

在联接的
WITH
子句中是否有访问联接实体关系的方法?我试图避免在子查询中使用
IN
子句

编辑:或者有没有一种方法可以在子查询上连接而不是在中使用

/**
 * @Entity
 * @Table(name="order_service_requests")
 */
class ServiceRequest
{
  const STATUS_REVIEW    = 0;
  const STATUS_PENDING   = 1;
  const STATUS_SCHEDULED = 2;
  const STATUS_COMPLETE  = 3;

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

  /**
   * @ManyToOne(targetEntity="Invoice")
   *
   * @var Invoice
   */
  protected $invoice;

  /**
   * @ManyToOne(targetEntity="ServiceType")
   * @JoinColumn(name="service_types_id")
   *
   * @var ServiceType
   */
  protected $serviceType;

  /**
   * @ManyToOne(targetEntity="Order")
   * @JoinColumn(name="orders_id")
   *
   * @var Order
   */
  protected $order;

  /**
   * @Column(type="integer")
   *
   * @var int
   */
  protected $status;

  /**
   * @Column(type="smallint", name="is_canceled")
   *
   * @var int
   */
  protected $isCanceled;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return \Entity\Invoice
   */
  public function getInvoice()
  {
    return $this->invoice;
  }

  /**
   * @return int
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function getStatus()
  {
    return $this->status;
  }

  /**
   * @param int $status
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function setStatus($status)
  {
    $this->status = $status;
  }

  /**
   * @return \Entity\ServiceType
   */
  public function getServiceType()
  {
    return $this->serviceType;
  }

  /**
   * @return int
   */
  public function isCanceled()
  {
    return $this->isCanceled;
  }

  public function getOrder()
  {
    return $this->order;
  }
}
i、 e.确保连接对象的
t.final
值为1

试图避免此查询

SELECT o
FROM Entity\Order o
WHERE o.status = :orderStatus
AND o.id NOT IN (
  SELECT o2.id
  FROM Entity\ServiceRequest s
  JOIN s.order o2
  JOIN s.serviceType t
  WHERE s.status = :serviceStatus
  AND t.final = 1
)
/**
 * @Entity
 * @Table(name="order_service_requests")
 */
class ServiceRequest
{
  const STATUS_REVIEW    = 0;
  const STATUS_PENDING   = 1;
  const STATUS_SCHEDULED = 2;
  const STATUS_COMPLETE  = 3;

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

  /**
   * @ManyToOne(targetEntity="Invoice")
   *
   * @var Invoice
   */
  protected $invoice;

  /**
   * @ManyToOne(targetEntity="ServiceType")
   * @JoinColumn(name="service_types_id")
   *
   * @var ServiceType
   */
  protected $serviceType;

  /**
   * @ManyToOne(targetEntity="Order")
   * @JoinColumn(name="orders_id")
   *
   * @var Order
   */
  protected $order;

  /**
   * @Column(type="integer")
   *
   * @var int
   */
  protected $status;

  /**
   * @Column(type="smallint", name="is_canceled")
   *
   * @var int
   */
  protected $isCanceled;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return \Entity\Invoice
   */
  public function getInvoice()
  {
    return $this->invoice;
  }

  /**
   * @return int
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function getStatus()
  {
    return $this->status;
  }

  /**
   * @param int $status
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function setStatus($status)
  {
    $this->status = $status;
  }

  /**
   * @return \Entity\ServiceType
   */
  public function getServiceType()
  {
    return $this->serviceType;
  }

  /**
   * @return int
   */
  public function isCanceled()
  {
    return $this->isCanceled;
  }

  public function getOrder()
  {
    return $this->order;
  }
}
重写尝试失败:
/**
 * @Entity
 * @Table(name="order_service_requests")
 */
class ServiceRequest
{
  const STATUS_REVIEW    = 0;
  const STATUS_PENDING   = 1;
  const STATUS_SCHEDULED = 2;
  const STATUS_COMPLETE  = 3;

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

  /**
   * @ManyToOne(targetEntity="Invoice")
   *
   * @var Invoice
   */
  protected $invoice;

  /**
   * @ManyToOne(targetEntity="ServiceType")
   * @JoinColumn(name="service_types_id")
   *
   * @var ServiceType
   */
  protected $serviceType;

  /**
   * @ManyToOne(targetEntity="Order")
   * @JoinColumn(name="orders_id")
   *
   * @var Order
   */
  protected $order;

  /**
   * @Column(type="integer")
   *
   * @var int
   */
  protected $status;

  /**
   * @Column(type="smallint", name="is_canceled")
   *
   * @var int
   */
  protected $isCanceled;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return \Entity\Invoice
   */
  public function getInvoice()
  {
    return $this->invoice;
  }

  /**
   * @return int
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function getStatus()
  {
    return $this->status;
  }

  /**
   * @param int $status
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function setStatus($status)
  {
    $this->status = $status;
  }

  /**
   * @return \Entity\ServiceType
   */
  public function getServiceType()
  {
    return $this->serviceType;
  }

  /**
   * @return int
   */
  public function isCanceled()
  {
    return $this->isCanceled;
  }

  public function getOrder()
  {
    return $this->order;
  }
}
无法访问
s.serviceType.final

SELECT o
FROM Entity\Order o
LEFT JOIN o.serviceRequests s
    WITH s.status = :serviceStatus
    AND s.serviceType.final = 1
LEFT JOIN s.serviceType t
WHERE o.status = :orderStatus
AND COUNT(s) = 0
订单实体:

<?php
namespace Entity;

/**
 * @Entity(repositoryClass="Repository\Order")
 * @Table(name="orders")
 */
class Order
{
  const STATUS_REVIEW    = 0;
  const STATUS_PENDING   = 1;
  const STATUS_SCHEDULED = 2;
  const STATUS_COMPLETE  = 3;

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

  /**
   * @ManyToOne(targetEntity="Invoice")
   *
   * @var Invoice
   */
  protected $invoice;

  /**
   * @Column(type="integer")
   *
   * @var int
   */
  protected $status;

  /**
   * @Column(type="smallint", name="is_canceled")
   *
   * @var int
   */
  protected $isCanceled;

  /**
   * @OneToMany(targetEntity="ServiceRequest", mappedBy="order")
   *
   * @var ServiceRequest[]
   */
  protected $serviceRequests;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return \Entity\Invoice
   */
  public function getInvoice()
  {
    return $this->invoice;
  }

  /**
   * @return int
   *
   * @uses \Entity\Order::STATUS_REVIEW
   * @uses \Entity\Order::STATUS_PENDING
   * @uses \Entity\Order::STATUS_SCHEDULED
   * @uses \Entity\Order::STATUS_COMPLETE
   */
  public function getStatus()
  {
    return $this->status;
  }

  /**
   * @param int $status
   *
   * @uses \Entity\Order::STATUS_REVIEW
   * @uses \Entity\Order::STATUS_PENDING
   * @uses \Entity\Order::STATUS_SCHEDULED
   * @uses \Entity\Order::STATUS_COMPLETE
   */
  public function setStatus($status)
  {
    $this->status = $status;
  }

  /**
   * @return int
   */
  public function getIsCanceled()
  {
    return $this->isCanceled;
  }

  public function cancel()
  {
    $this->isCanceled = 1;
  }

  /**
   * @return ServiceRequest[]
   */
  public function getServices()
  {
    return $this->services;
  }
}
/**
 * @Entity
 * @Table(name="order_service_requests")
 */
class ServiceRequest
{
  const STATUS_REVIEW    = 0;
  const STATUS_PENDING   = 1;
  const STATUS_SCHEDULED = 2;
  const STATUS_COMPLETE  = 3;

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

  /**
   * @ManyToOne(targetEntity="Invoice")
   *
   * @var Invoice
   */
  protected $invoice;

  /**
   * @ManyToOne(targetEntity="ServiceType")
   * @JoinColumn(name="service_types_id")
   *
   * @var ServiceType
   */
  protected $serviceType;

  /**
   * @ManyToOne(targetEntity="Order")
   * @JoinColumn(name="orders_id")
   *
   * @var Order
   */
  protected $order;

  /**
   * @Column(type="integer")
   *
   * @var int
   */
  protected $status;

  /**
   * @Column(type="smallint", name="is_canceled")
   *
   * @var int
   */
  protected $isCanceled;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return \Entity\Invoice
   */
  public function getInvoice()
  {
    return $this->invoice;
  }

  /**
   * @return int
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function getStatus()
  {
    return $this->status;
  }

  /**
   * @param int $status
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function setStatus($status)
  {
    $this->status = $status;
  }

  /**
   * @return \Entity\ServiceType
   */
  public function getServiceType()
  {
    return $this->serviceType;
  }

  /**
   * @return int
   */
  public function isCanceled()
  {
    return $this->isCanceled;
  }

  public function getOrder()
  {
    return $this->order;
  }
}
<?php
namespace Entity;

/**
 * @Entity
 * @Table(name="service_types")
 */
class ServiceType
{
  /**
   * @Id
   * @Column(type="integer")
   * @GeneratedValue
   *
   * @var int
   */
  protected $id;

  /**
   * @Column(type="smallint")
   *
   * @var int
   */
  protected $final;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return int
   */
  public function getFinal()
  {
    return $this->final;
  }
}
服务类型实体:

<?php
namespace Entity;

/**
 * @Entity(repositoryClass="Repository\Order")
 * @Table(name="orders")
 */
class Order
{
  const STATUS_REVIEW    = 0;
  const STATUS_PENDING   = 1;
  const STATUS_SCHEDULED = 2;
  const STATUS_COMPLETE  = 3;

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

  /**
   * @ManyToOne(targetEntity="Invoice")
   *
   * @var Invoice
   */
  protected $invoice;

  /**
   * @Column(type="integer")
   *
   * @var int
   */
  protected $status;

  /**
   * @Column(type="smallint", name="is_canceled")
   *
   * @var int
   */
  protected $isCanceled;

  /**
   * @OneToMany(targetEntity="ServiceRequest", mappedBy="order")
   *
   * @var ServiceRequest[]
   */
  protected $serviceRequests;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return \Entity\Invoice
   */
  public function getInvoice()
  {
    return $this->invoice;
  }

  /**
   * @return int
   *
   * @uses \Entity\Order::STATUS_REVIEW
   * @uses \Entity\Order::STATUS_PENDING
   * @uses \Entity\Order::STATUS_SCHEDULED
   * @uses \Entity\Order::STATUS_COMPLETE
   */
  public function getStatus()
  {
    return $this->status;
  }

  /**
   * @param int $status
   *
   * @uses \Entity\Order::STATUS_REVIEW
   * @uses \Entity\Order::STATUS_PENDING
   * @uses \Entity\Order::STATUS_SCHEDULED
   * @uses \Entity\Order::STATUS_COMPLETE
   */
  public function setStatus($status)
  {
    $this->status = $status;
  }

  /**
   * @return int
   */
  public function getIsCanceled()
  {
    return $this->isCanceled;
  }

  public function cancel()
  {
    $this->isCanceled = 1;
  }

  /**
   * @return ServiceRequest[]
   */
  public function getServices()
  {
    return $this->services;
  }
}
/**
 * @Entity
 * @Table(name="order_service_requests")
 */
class ServiceRequest
{
  const STATUS_REVIEW    = 0;
  const STATUS_PENDING   = 1;
  const STATUS_SCHEDULED = 2;
  const STATUS_COMPLETE  = 3;

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

  /**
   * @ManyToOne(targetEntity="Invoice")
   *
   * @var Invoice
   */
  protected $invoice;

  /**
   * @ManyToOne(targetEntity="ServiceType")
   * @JoinColumn(name="service_types_id")
   *
   * @var ServiceType
   */
  protected $serviceType;

  /**
   * @ManyToOne(targetEntity="Order")
   * @JoinColumn(name="orders_id")
   *
   * @var Order
   */
  protected $order;

  /**
   * @Column(type="integer")
   *
   * @var int
   */
  protected $status;

  /**
   * @Column(type="smallint", name="is_canceled")
   *
   * @var int
   */
  protected $isCanceled;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return \Entity\Invoice
   */
  public function getInvoice()
  {
    return $this->invoice;
  }

  /**
   * @return int
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function getStatus()
  {
    return $this->status;
  }

  /**
   * @param int $status
   *
   * @uses \Entity\ServiceRequest::STATUS_REVIEW
   * @uses \Entity\ServiceRequest::STATUS_PENDING
   * @uses \Entity\ServiceRequest::STATUS_SCHEDULED
   * @uses \Entity\ServiceRequest::STATUS_COMPLETE
   */
  public function setStatus($status)
  {
    $this->status = $status;
  }

  /**
   * @return \Entity\ServiceType
   */
  public function getServiceType()
  {
    return $this->serviceType;
  }

  /**
   * @return int
   */
  public function isCanceled()
  {
    return $this->isCanceled;
  }

  public function getOrder()
  {
    return $this->order;
  }
}
<?php
namespace Entity;

/**
 * @Entity
 * @Table(name="service_types")
 */
class ServiceType
{
  /**
   * @Id
   * @Column(type="integer")
   * @GeneratedValue
   *
   * @var int
   */
  protected $id;

  /**
   * @Column(type="smallint")
   *
   * @var int
   */
  protected $final;

  /**
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }

  /**
   * @return int
   */
  public function getFinal()
  {
    return $this->final;
  }
}

您在加入servicetype之前查询它。尝试:

SELECT o
FROM Entity\Order o
LEFT JOIN o.serviceRequests s
  WITH s.status = :serviceStatus
LEFT JOIN s.serviceType t
WHERE o.status = :orderStatus
AND COUNT(s) = 0
AND t.final = 1

这会消除任何有“最终”服务请求的订单吗?每个订单有多个服务请求。与普通SQL相比,我对使用DQL还是有点陌生。很抱歉,基于您修改的查询,我原以为您想要所有的最终版本。如果要消除t.final为1(布尔字段?)的所有情况,请在上述查询中设置t.final=0。