1

我的数据库中的表、表Students和表之间存在多对多关系。Programsstudentprogramstudent_program

我正在尝试加入这两个实体并执行一些需要子查询的自定义查询。这意味着 Doctrine QueryBuilder 不能工作,因为它不支持子查询。

相反,我正在尝试 NativeSQL 功能,并且取得了不错的进展。但是,当我尝试SELECTProgram实体中获取某些内容时,我收到错误消息Notice: Undefined index: Bundle\Entity\Program in vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line 180

    $mapping = new \Doctrine\ORM\Query\ResultSetMappingBuilder($em);
    $mapping->addRootEntityFromClassMetadata('Student', 's');
    $mapping->addJoinedEntityFromClassMetadata('Program', 'p', 's', 'programs', array('id' => 'program_id'));
    // Query based on form 
    $sql = 'SELECT s.id, s.last_name, p.name <---- problem when this is added
            FROM student s
            JOIN program p
            ';

    $query = $em->createNativeQuery($sql, $mapping);

    $students = $query->getResult();
4

1 回答 1

0

不是直接答案,但学说 2 确实支持子查询。只需创建一个查询,然后将 dql 输入 where 类。这个例子有点冗长,但它工作得很好:

public function queryGames($search)
{
    // Pull params
    $ages    = $this->getValues($search,'ages');
    $genders = $this->getValues($search,'genders');
    $regions = $this->getValues($search,'regions');

    $sortBy  = $this->getValues($search,'sortBy',1);
    $date1   = $this->getValues($search,'date1');
    $date2   = $this->getValues($search,'date2');
    $time1   = $this->getValues($search,'time1');
    $time2   = $this->getValues($search,'time2');

    $projectId = $this->getValues($search,'projectId');

    // Build query
    $em = $this->getEntityManager();
    $qbGameId = $em->createQueryBuilder(); // ### SUB QUERY ###

    $qbGameId->addSelect('distinct gameGameId.id');

    $qbGameId->from('ZaysoCoreBundle:Event','gameGameId');

    $qbGameId->leftJoin('gameGameId.teams',   'gameTeamGameId');
    $qbGameId->leftJoin('gameTeamGameId.team','teamGameId');

    if ($projectId) $qbGameId->andWhere($qbGameId->expr()->in('gameGameId.projectId',$projectId));

    if ($date1) $qbGameId->andWhere($qbGameId->expr()->gte('gameGameId.date',$date1));
    if ($date2) $qbGameId->andWhere($qbGameId->expr()->lte('gameGameId.date',$date2));

    if ($time1) $qbGameId->andWhere($qbGameId->expr()->gte('gameGameId.time',$time1));
    if ($time2) $qbGameId->andWhere($qbGameId->expr()->lte('gameGameId.time',$time2));

    if ($ages)    $qbGameId->andWhere($qbGameId->expr()->in('teamGameId.age',   $ages));
    if ($genders) $qbGameId->andWhere($qbGameId->expr()->in('teamGameId.gender',$genders));

    if ($regions) 
    {
        // $regions[] = NULL;
        // $qbGameId->andWhere($qbGameId->expr()->in('teamGameId.org',   $regions));

        $qbGameId->andWhere($qbGameId->expr()->orX(
            $qbGameId->expr()->in('teamGameId.org',$regions),
            $qbGameId->expr()->isNull('teamGameId.org')
        ));

    }
    //$gameIds = $qbGameId->getQuery()->getArrayResult();
    //Debug::dump($gameIds);die();
    //return $gameIds;

    // Games
    $qbGames = $em->createQueryBuilder();

    $qbGames->addSelect('game');
    $qbGames->addSelect('gameTeam');
    $qbGames->addSelect('team');
    $qbGames->addSelect('field');

    $qbGames->addSelect('gamePerson');
    $qbGames->addSelect('person');

    $qbGames->from('ZaysoCoreBundle:Event','game');

    $qbGames->leftJoin('game.teams',   'gameTeam');
    $qbGames->leftJoin('game.persons', 'gamePerson');
    $qbGames->leftJoin('game.field',   'field');

    $qbGames->leftJoin('gameTeam.team',     'team');
    $qbGames->leftJoin('gamePerson.person', 'person');

    $qbGames->andWhere($qbGames->expr()->in('game.id',$qbGameId->getDQL())); // ### THE TRICK ###

    switch($sortBy)
    {
        case 1:
            $qbGames->addOrderBy('game.date');
            $qbGames->addOrderBy('game.time');
            $qbGames->addOrderBy('field.key1');
            break;
        case 2:
            $qbGames->addOrderBy('game.date');
            $qbGames->addOrderBy('field.key1');
            $qbGames->addOrderBy('game.time');
            break;
        case 3:
            $qbGames->addOrderBy('game.date');
            $qbGames->addOrderBy('team.age');
            $qbGames->addOrderBy('game.time');
            $qbGames->addOrderBy('field.key1');
            break;
    }

    // Always get an array even if no records found
    $query = $qbGames->getQuery();
    $items = $query->getResult();

    return $items;
}
于 2013-01-30T01:49:24.840 回答