Symfony1 如何控制关系查询的顺序
Symfony/doctrine中有一个名为JosVmOrderHistory的模型有什么魔力吗 我有这个schema.yml摘录:Symfony1 如何控制关系查询的顺序,symfony1,doctrine,schema,history,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
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始终按顺序_status _history _id的降序排列。我甚至将其添加到model/doctrine/JosVmOrderHistoryTable.class.php中:
public function createQuery($alias){
return parent::createQuery($alias)->orderBy('order_status_history_id asc');
}
但它是按降序排列的
编辑:我已经更新了主题-这个问题似乎归结为如何控制关系查询的OrderBy。createQuery似乎根本没有被调用。如果出于速度原因需要,您不应该从数据库加载整个订单历史记录,如果您只对第一个订单历史记录感兴趣,请使用如下查询:
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 }
您是否考虑过只对数组进行排序,而不是尝试更改规则的行为方式?否则,直接查询相关数据,而不是通过另一个对象上的getter访问它。出于速度原因,我需要这样做,基本上关系需要包含在一个table_方法中,自定义排序需要工作。我现在构建了一个MySQL视图,并为此创建了一个关系。谢谢,我得出了相同的结论。这里的速度问题不是通过调用getPaymentStatus()来解决的,而是一个表方法,它需要一个表子查询(请参阅)。最后,我使用了一个MySQL视图,并将其加入到table_方法中。也就是说,您能详细说明我的侦听器函数将位于何处吗?我将侦听器实现为一种行为,但我认为,如果您只希望它用于这一个类,它将在设置期间进入该类。与setTableDefinition()函数类似,使用$this->addListener()添加侦听器。侦听器本身需要是它自己的类。如果您只为这个类使用,那么只需将它的定义放在同一个文件中。listener类应该扩展Doctrine_Record_listener并实现preDqlSelect()函数。对于preDqlSelect函数,您可以使用如下代码:$query=$event->getQuery()$orderby=$query->getDqlPart('orderby');//如果(count($orderby)==0){$query->orderby(“order_status_history_id”)}未设置其他排序,则仅应用默认排序