Symfony 排序模型有请求
我有两个模型:操作和操作历史记录
Operation:
columns:
startdate: { type: timestamp, notnull: true }
enddate: { type: timestamp, notnull: true }
year: { type: integer, notnull: true }
abscomment: { type: string(500) }
person_id: { type: integer, notnull: true }
operationtype_id: { type: integer, notnull: true }
relations:
Person: { onDelete: CASCADE, local: person_id, foreign: id, foreignAlias: Operations}
OperationType: { onDelete: CASCADE, local: absencetype_id, foreign: id, foreignAlias: Operations }
OperationHistory:
columns:
oh_comment: { type: string(500), notnull: true }
oh_date: { type: timestamp, notnull: true }
status_id: { type: integer, notnull: true }
operation_id: { type: integer, notnull: true }
updater: { type: integer, notnull: true }
indexes:
date:
fields:
ohdate:
sorting: DESC
relations:
Operation: { onDelete: CASCADE, local: operation_id, foreign: id, foreignAlias: OperationsHistory }
Person: { onDelete: CASCADE, local: updater, foreign: id, foreignAlias: OperationsHistory }
OperationStatus: { onDelete: CASCADE, local: status_id, foreign: id, foreignAlias: OperationsHistory }
为了获取所有按人员操作,我在操作历史记录上使用索引。因此,如果我需要具有特定状态的所有人员的操作:
public function getOperations($person, $status){
$q = $this->createQuery('o')
->leftJoin('o.OperationsHistory oh')
->leftJoin('p.Person p')
->groupBy('ah.absence_id')
->having('ah.status_id = ?', $status);
return $q->execute();
}
由于 ohdate 上的索引,我假设使用 groupBy,我只能获得有关特定操作的最后一个操作历史记录。没有having子句,这很好,但我只想要具有特定状态的操作。但如果我设置这个having子句,我什么也得不到。
事实上,我需要翻译这个 sql 请求:
SELECT *
FROM operation o
LEFT JOIN (
SELECT *
FROM operation_history
ORDER BY ohdate DESC
) oh ON o.id = oh.absence_id
LEFT JOIN person p ON p.id = o.person_id
WHERE p.id = 1
GROUP BY oh.operation_id
HAVING oh.status_id = 1
抱歉我的英语不好,我希望这些信息对我有用。
感谢你 !
I have two models : Operation and OperationHistory
Operation:
columns:
startdate: { type: timestamp, notnull: true }
enddate: { type: timestamp, notnull: true }
year: { type: integer, notnull: true }
abscomment: { type: string(500) }
person_id: { type: integer, notnull: true }
operationtype_id: { type: integer, notnull: true }
relations:
Person: { onDelete: CASCADE, local: person_id, foreign: id, foreignAlias: Operations}
OperationType: { onDelete: CASCADE, local: absencetype_id, foreign: id, foreignAlias: Operations }
OperationHistory:
columns:
oh_comment: { type: string(500), notnull: true }
oh_date: { type: timestamp, notnull: true }
status_id: { type: integer, notnull: true }
operation_id: { type: integer, notnull: true }
updater: { type: integer, notnull: true }
indexes:
date:
fields:
ohdate:
sorting: DESC
relations:
Operation: { onDelete: CASCADE, local: operation_id, foreign: id, foreignAlias: OperationsHistory }
Person: { onDelete: CASCADE, local: updater, foreign: id, foreignAlias: OperationsHistory }
OperationStatus: { onDelete: CASCADE, local: status_id, foreign: id, foreignAlias: OperationsHistory }
In order to get all Operation by Person, I use an index on OperationHistory. So if I need all the person's operation with a specific status:
public function getOperations($person, $status){
$q = $this->createQuery('o')
->leftJoin('o.OperationsHistory oh')
->leftJoin('p.Person p')
->groupBy('ah.absence_id')
->having('ah.status_id = ?', $status);
return $q->execute();
}
Thanks to the index on ohdate, I assume with the groupBy, I only get the last OperationHistory about a specific Operation. Without the having clause, It's good, but I only want Operations with a specific Status. But if I set this having clause, I get nothing at all.
In fact, I need to translate this sql request :
SELECT *
FROM operation o
LEFT JOIN (
SELECT *
FROM operation_history
ORDER BY ohdate DESC
) oh ON o.id = oh.absence_id
LEFT JOIN person p ON p.id = o.person_id
WHERE p.id = 1
GROUP BY oh.operation_id
HAVING oh.status_id = 1
sorry for my bad english and I hope these informations will be usefull to help me.
Thank u !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅根据您的上述问题很难理解您想要做什么 - 您可以提供一些其他信息吗?
您可以使用 Doctrine_Query 类的
join
和orderby
方法:Its very difficult to understand what you are trying to do just based on your questions above - can you include some other information ?
You can use
join
andorderby
methods of the Doctrine_Query class :通过使用 Doctrine_RawSql(),它可以工作!
By using Doctrine_RawSql(), it works !