如何控制关系查询的顺序

发布于 2024-11-25 00:53:07 字数 2848 浏览 1 评论 0原文

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 的foreignAlias 是“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 似乎根本没有被调用。

Is there some magic in Symfony/doctrine with a model called JosVmOrderHistory?

I have this schema.yml excerpt:

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 }

Firstly, the foreignAlias of JosVmOrderHistory is "JosVmOrderHistory", not "JosVmOrderHistoryList" as I would have assumed.

Secondly, in model/doctrine/Order.class.php, I have the following function:

function getPaymentStatus(){

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

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

$historyList is always in descending order of order_status_history_id. I even added to model/doctrine/JosVmOrderHistoryTable.class.php:

public function createQuery($alias){

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


}

but it stays in descending order..

Edit: I've updated the subject - this problem seems to come down to how to control the OrderBy of a relation query. createQuery doesn't seem to be called at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-12-02 00:53:07

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

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 }

If you need this for speed reasons you shouldnt load the entire order history from the DB if you are just interested in the first one, instead use a query like:

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;
    }
}

That being said, it is possible to automatically affect the sorting more like what you were trying to do by using a listener. In the past, I have created a generic sort behavior that you can use like:

  actAs:
    DefaultSort: { column: title }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文