Symfony1 具有请求的Symfony排序模型
我有两个模型:操作和操作历史Symfony1 具有请求的Symfony排序模型,symfony1,doctrine,symfony-1.4,Symfony1,Doctrine,Symfony 1.4,我有两个模型:操作和操作历史 Operation: columns: startdate: { type: timestamp, notnull: true } enddate: { type: timestamp, notnull: true } year: { type: integer, notnull: true } abscomment: { type: string(500) } pers
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 }
为了按个人获取所有操作,我使用了OperationHistory索引。因此,如果我需要所有具有特定状态的人员操作:
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
很抱歉我的英语不好,我希望这些信息能对我有所帮助
谢谢你 仅仅根据上面的问题很难理解你想做什么-你能提供一些其他信息吗 您可以使用Doctrine_查询类的join和orderby方法:
$query = Doctrine_Core::getTable('operation')->createQuery()
->innerjoin('operation_history ......')
->orderby('.....')
通过使用SQL,它可以工作
public function getAbsenceQueries($person, $status){
$q = new Doctrine_RawSql();
$q->select('{o.*}, {oh.*}')
->from('operation o LEFT JOIN (SELECT * from operation_history order by ohdate DESC) oh ON o.id=oh.operation_id LEFT JOIN person p ON p.id = o.person_id')
->where('mg.group_id = ?', $group)
->groupBy('ah.absence_id')
->having('ah.status_id = ?', $status)
->addComponent('o','Operation o')
->addComponent('oh','o.OperationsHistory oh')
->addComponent('p','o.Person p');
return $q->execute();
}
尝试->where而不是having,但是这里我需要一个having子句,而不是where.in mysql中的OK,除了group by之外,因为操作历史不是order by ohdate。我认为在这里它不起作用,因为排序模型。