如何控制关系查询的顺序

时间:2011-07-19 10:26:08

标签: symfony1 doctrine schema history

Symfony / doctrine中有一个名为JosVmOrderHistory的模型吗?

我有这个schema.yml摘录:

JosVmOrder:
  tableName: jos_vm_orders
  columns:
    order_id: { type: integer(4), primary: true, autoincrement: true }
    user_id: { type: integer(4), notnull: true }
    vendor_id: { type: integer(4), notnull: true }
    order_number: { type: string(32), notnull: false }
    user_info_id: { type: string(32), notnull: false }
    order_total: { type: 'decimal(15, 5)', notnull: true }
    order_subtotal: { type: 'decimal(15, 5)', notnull: false }
    order_tax: { type: 'decimal(10, 2)', notnull: false }
    order_tax_details: { type: string(), notnull: true }
    order_shipping: { type: 'decimal(10, 2)', notnull: false }
    order_shipping_tax: { type: 'decimal(10, 2)', notnull: false }
    coupon_discount: { type: 'decimal(12, 2)', notnull: true }
    coupon_code: { type: string(32), notnull: false }
    order_discount: { type: 'decimal(12, 2)', notnull: true }
    order_currency: { type: string(16), notnull: false }
    order_status: { type: string(1), notnull: false }
    cdate: { type: integer(4), notnull: false }
    mdate: { type: integer(4), notnull: false }
    ship_method_id: { type: string(255), notnull: false }
    customer_note: { type: string(), notnull: true }
    ip_address: { type: string(15), notnull: true }
  relations:
    User: { class: JosUser, local: user_id, foreignAlias: OrderList }
    LatestVmOrderDetail: { class: LatestVmOrderDetail, local: order_id, foreign: order_id }

JosVmOrderHistory:
  tableName: jos_vm_order_history
  columns:
    order_status_history_id: { type: integer(4),primary: true, autoincrement: true }
    order_id: { type: integer(4) }
    order_status_code: { type: string(1) }
    date_added: { type: timestamp(25) }
    customer_notified: { type: integer(4), notnull: false }
    comments: { type: clob(16777777), notnull: false }
  relations:
    Order: { class: JosVmOrder, local: order_id, foreign: order_id }
    OrderStatus: { class: JosVmOrderStatus, local: order_status_code, foreign: order_status_code }

首先,JosVmOrderHistory的外国人是“JosVmOrderHistory”,而不是我所假设的“JosVmOrderHistoryList”。

其次,在model / doctrine / Order.class.php中,我有以下功能:

function getPaymentStatus(){

    $historyList = $this->getJosVmOrderHistory();
    if (count($historyList)){

        return $paymentStatus = $historyList[0];
    }else{
        return $paymentStatus = null;
    }
}

$ historyList始终按order_status_history_id的降序排列。我甚至加入了model / doctrine / JosVmOrderHistoryTable.class.php:

public function createQuery($alias){

    return parent::createQuery($alias)->orderBy('order_status_history_id asc');


}

但它按降序排列..

编辑:我已经更新了主题 - 这个问题似乎归结为如何控制关系查询的OrderBy。似乎根本没有调用createQuery。

1 个答案:

答案 0 :(得分:1)

如果出于速度原因需要这个,如果您只对第一个订单感兴趣,则不应从数据库加载整个订单历史记录,而是使用如下查询:

function getPaymentStatus(){

    $historyList = Doctrine::getTable('JosVmOrderHistory')->createQuery()->where('order_id = ?', $this->order_id)->orderBy('order_status_history_id')->limit(1)->execute();
    if (count($historyList)){

        return $paymentStatus = $historyList[0];
    }else{
        return $paymentStatus = null;
    }
}

话虽这么说,但是可以通过使用监听器自动影响排序,就像你试图做的那样。在过去,我创建了一个可以使用的通用排序行为:

  actAs:
    DefaultSort: { column: title }