1

データベース内のテーブル、、およびStudentsとの間に多対多の関係があります。Programsstudentprogramstudent_program

2 つのエンティティを結合し、サブクエリを必要とするいくつかのカスタム クエリを実行しようとしています。これは、サブクエリをサポートしていないため、Doctrine QueryBuilder が機能しないことを意味します。

代わりに、私は NativeSQL 関数を試しており、まともな進歩を遂げています。ただし、エンティティからSELECT何かをしようとすると、エラーが発生します。ProgramNotice: 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 に答える