PHP Doctrine - 在一个Join中选择Annidate,如何与其他查询

时间:2016-09-07 10:51:49

标签: php symfony doctrine

我是PHP Doctrine的新手,我遇到了一些问题! 我在处理器

中有这个查询

SELECT h2.Date AS CalDate, h2.Manager_Id, h2.ProjectCode, h2.Project, h2.Name, h2.CN, h2.Description, h2.HourType, h2.Hours AS HoursAdj, h2.Correction 
    FROM Calendar cal2 JOIN
    		(SELECT cn2.CN_id, cn2.CN, cn2.Description, p2.Name, hr2.Hours,  hr2.Correction, hr2.Date, ht2.Name AS HourType, prj2.Manager_Id, prj2.Name AS Project, prj2.ProjectCode 
    FROM Hours hr2
    		RIGHT JOIN CN cn2 ON hr2.CN_id = cn2.CN_id 
    		RIGHT JOIN Persons p2 ON cn2.Persons_id = p2.Persons_id
    		JOIN HourType ht2 ON hr2.HourType_Id = ht2.HourType_Id
    		JOIN Projects prj2 ON hr2.Projects_Id = prj2.Projects_Id) h2 ON cal2.date = h2.Date
    		WHERE h2.Date BETWEEN '$dateFrom' AND '$dateTo' AND Correction = 1

if ($project !== null && $project !== "ALL")
		$query .= " AND h2.ProjectCode = '$project'";
	else if ($pm !== null && $pm !== "00000")
		$query .= " AND h2.Manager_Id = '$pm'";
	if ($consultant !== null && $consultant !== "ALL")
		$query .= " AND h2.CN_id = '$consultant'";

我试图解决这个分离2查询的问题。 内部的一个:

$qb_int_1 = $this->defaultEntityManager->createQueryBuilder();
            $qb_int_1 ->select('cn2.cnId', 'cn2.cn', 'cn2.description', 'p2.name', 'hr2.hours', 'hr2.correction', 'hr2.date', 'ht2.name AS hourType', 'prj2.managerId', 'prj2.name AS projectName', 'prj2.projectcode')
                    ->from('TimereportdbBundle:Persons', 'p2')
                    ->leftJoin('TimereportdbBundle:Cn', 'cn2', 'WITH', 'cn2.personsId = p2.personsId')
                    ->leftJoin('TimereportdbBundle:Hours', 'hr2', 'WITH', 'hr2.cnId = cn2.cnId')
                    ->join('TimereportdbBundle:HourType', 'ht2', 'WITH', 'hr2.hourtypeId = ht2.hourtypeId')
                    ->join('TimereportdbBundle:Projects', 'prj2', 'WITH', 'hr2.projectsId = prj2.projectsId');

之后,我尝试以这种方式解决外部问题:

$qb_adjReport_1 = $this->defaultEntityManager->createQueryBuilder();
            $qb_adjReport_1->select('h2.date AS calDate', 'h2.managerId', 'h2.projectcode', 'h2.project', 'h2.name', 'h2.cn', 'h2.description', 'h2.hourType', 'h2.Hours AS hoursAdj', 'h2.correction')
               ->from('TimereportdbBundle:Calendar', 'cal2')
               ->join($qb_int_1, 'h2', 'WITH', 'cal2.date', 'h2.Date')
               ->where('h2.date BETWEEN :dateFrom AND :dateTo')
                    ->andwhere('h2.correction = 1')
               ->setParameter('dateFrom', $dateFrom)
               ->setParameter('dateTo', $dateTo);     
            if ($projectcode !== null && $projectcode !== "ALL"){
                $qb_adjReport_1->andwhere('h2.projectcode = :projectcode')
                ->setParameter('projectcode', $projectcode);
            }
            else if ($pm !== null && $pm !== "00000") {
                    $qb_adjReport_1->andwhere('h2.managerId = :managerId')
                ->setParameter('managerId', $pmId);
                }
            if($consultant !== null && $consultant !== "ALL"){
                $qb_adjReport_1->andwhere('h2.cn = :consultant')
                ->setParameter('consultant', $consultant);
            }
            
            $adjReports1 = $qb_adjReport_1->getQuery()->getResult(); 

我试图用这种方式解决查询这个2链接 enter link description here

enter link description here

这是在最后一条getResult()指令上收到的错误: Doctrine \ ORM \ Query \ QueryException:[语义错误]第0行,第199页'SELECT cn2.cnId'附近:'错误:未定义类'SELECT'。

提前致谢。

0 个答案:

没有答案