Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 本机SQL多对多查询原则_Mysql_Symfony_Doctrine - Fatal编程技术网

Mysql 本机SQL多对多查询原则

Mysql 本机SQL多对多查询原则,mysql,symfony,doctrine,Mysql,Symfony,Doctrine,我的数据库中有学生和程序之间的多对多关系,其中有学生、程序和学生程序 我正在尝试连接这两个实体,并执行一些需要子查询的自定义查询。这意味着条令QueryBuilder无法工作,因为它不支持子查询 相反,我正在尝试NativeSQL函数,并取得了不错的进展。但是,当我尝试从程序实体中选择时,我得到了错误注意:在vendor/doctor/orm/lib/doctor/orm/Internal/Hydration/ObjectHydrator.php第180行中未定义的索引:Bundle\entit

我的数据库中有
学生
程序
之间的多对多关系,其中有
学生
程序
学生程序

我正在尝试连接这两个实体,并执行一些需要子查询的自定义查询。这意味着条令QueryBuilder无法工作,因为它不支持子查询

相反,我正在尝试NativeSQL函数,并取得了不错的进展。但是,当我尝试从
程序
实体中选择
时,我得到了错误
注意:在vendor/doctor/orm/lib/doctor/orm/Internal/Hydration/ObjectHydrator.php第180行
中未定义的索引:Bundle\entity\Program

    $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();
$mapping=new\Doctrine\ORM\Query\resultstmappingbuilder($em);
$mapping->addRootEntityFromClassMetadata('Student','s');
$mapping->addJoinedEntityFromClassMetadata('Program'、'p'、's'、'programs',array('id'=>'Program_id'));
//基于表单的查询

$sql='SELECT s.id,s.last_name,p.name不是直接答案,但原则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;
}