如何从Doctrine和Symfony获得汇总标量结果?

时间:2016-01-29 02:27:27

标签: php symfony doctrine-orm

如何将Person,Application,Scalar A作为结果集?

我目前正在使用[0] =>获取数组人[1] =>应用程序等。“addScalarResult()”调用似乎被忽略了。

public function exportHR63Status(Semester $semester) {
  $resultSetMapping = new ResultSetMappingBuilder($this->getEntityManager());
  $resultSetMapping->addRootEntityFromClassMetadata('Unsw\CamsBundle\Entity\Person', 'p');
  $resultSetMapping->addRootEntityFromClassMetadata('Unsw\CamsBundle\Entity\Application', 'a',
  array(
    'id' => 'application_id',
    'deleted' => 'application_deleted',
    'created' => 'application_created',
    'updated' => 'application_updated'
  ));

  $selectClause = $resultSetMapping->generateSelectClause();

  $resultSetMapping->addScalarResult('signedCasual', 'signedCasual');
  $resultSetMapping->addScalarResult('signedFinance', 'signedFinance');
  $resultSetMapping->addScalarResult('signedHeadOfSchool', 'signedHeadOfSchool');

  $q = $this->getEntityManager()->createNativeQuery("SELECT " . $selectClause . ",
    EXISTS (select 1 from hr63signer_hr63submission hshs INNER JOIN hr63signer hs ON hshs.hr63signer_id = hs.id WHERE h1_.id = hshs.hr63submission_id and hs.signertype = 'casual') as signedCasual,
    EXISTS (select 1 from hr63signer_hr63submission hshs INNER JOIN hr63signer hs ON hshs.hr63signer_id = hs.id WHERE h1_.id = hshs.hr63submission_id and hs.signertype = 'finance') as signedFinance,
    EXISTS (select 1 from hr63signer_hr63submission hshs INNER JOIN hr63signer hs ON hshs.hr63signer_id = hs.id WHERE h1_.id = hshs.hr63submission_id and hs.signertype = 'headofschool') as signedHeadOfSchool
    -- processed finance is hr63_submission.processed_finance
    FROM hr63_submission h1_
    INNER JOIN course c2_ ON h1_.course_id = c2_.id
    INNER JOIN application_courses ac ON c2_.id = ac.course_id
    INNER JOIN application a ON ac.application_id = a.id
    INNER JOIN person p ON h1_.person_id = p.znumber
    WHERE c2_.semester_id = 14 -- either course or application should give the same result", $resultSetMapping);

  $q->setParameter('semester_id', $semester->getId());

  return $q->getResult();
}

Symfony 2.7.6,Doctrine 2.5.1

1 个答案:

答案 0 :(得分:0)

答案是我将semester_id的参数硬编码为14并绑定了一个不匹配的参数。改变

c2_.semester_id = 14

c2_.semester_id = :semester_id

给予:

[0] Person
[1] Application plus scalar entries.