2011-10-11 52 views
0

我有两个模式:操作和OperationHistorySymfony的分类模型具有要求

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

2009年,由于ohdate指标,我与GROUPBY承担,我只得到有关特定操作的最后OperationHistory。没有条款,这很好,但我只想要具有特定状态的操作。但是如果我设置了这个条款,我什么也得不到。

其实,我需要把这种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 

对不起我的英文不好,我希望这些信息将是有用的帮助我。

谢谢你!

回答

0

基于上述问题,您很难理解您正在尝试做什么 - 您能否包含其他一些信息?

可以使用Doctrine_Query类joinorderby方法:

$query = Doctrine_Core::getTable('operation')->createQuery() 
->innerjoin('operation_history ......') 
->orderby('.....') 
+0

我更新了我的问题! –

+0

尝试 - >在哪里()而不是有 – ManseUK

+0

但在这里我需要有一个条款,而不是在哪里。在mysql中为 –

0

通过使用Doctrine_RawSql(),它的工作原理!

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