来自查询本机构建器symfony6的空数组

k2fxgqgv  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(115)

我是symfony 6.1的新手,我想知道我的自定义sql请求有什么问题。我尝试了很多方法,但都没有成功,你能帮助我吗?
这是我的Accueil控制器,我想从存储库中取回SQL结果:

<?php

namespace App\Controller;

use App\Entity\Mission;
use App\Entity\Tag;
use App\Entity\User;
use App\Form\AddMissionFormType;
use App\Form\RegistrationFormType;
use App\Repository\MissionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class AccueilProspectorController extends AbstractController
{
    #[Route('/accueil/prospector', name: 'app_accueil_prospector')]
    public function index(Request $request,ManagerRegistry $doctrine,Security $security): Response
    {
        $mission = new Mission();

        //Récupération de toutes les missions.
        $allmission = $doctrine->getManager()->getRepository(Mission::class)->selectmissionswithtags();

        //Création du formulaire pour ajouter une mission

        $mission->setIduser($security->getUser());
        $form = $this->createForm(AddMissionFormType::class, $mission)->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()){
            $entityManager = $doctrine->getManager();
            $entityManager->persist($mission);
            $entityManager->flush();
            return $this->redirectToRoute('app_accueil_prospector');
        }

        return $this->render('accueil_prospector/index.html.twig', [
            'controller_name' => 'AccueilProspectorController',
            'addmissionForm' => $form->createView(),
            'missionsvalues' => $allmission,
        ]);
    }
}

这是我的存储库,请求在哪里:

<?php

namespace App\Repository;

use App\Entity\Mission;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Mission>
 *
 * @method Mission|null find($id, $lockMode = null, $lockVersion = null)
 * @method Mission|null findOneBy(array $criteria, array $orderBy = null)
 * @method Mission[]    findAll()
 * @method Mission[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class MissionRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Mission::class);
    }

    public function save(Mission $entity, bool $flush = false): void
    {
        $this->getEntityManager()->persist($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(Mission $entity, bool $flush = false): void
    {
        $this->getEntityManager()->remove($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function selectmissionswithtags(){
        $sql = "SELECT descriptionmission,onsetdate,deadline,prioritymission,remote, GROUP_CONCAT(tg.nomtag SEPARATOR ',') as tag From mission m
                left join mission_tag mt on m.id = mt.mission_id
                left join tag tg on mt.tag_id = tg.id
                GROUP BY descriptionmission;";
        $rsm = new ResultSetMapping();
        $rsm->addEntityResult(Mission::class, 'mission');
        $em = $this->getEntityManager();
        return $result = $em->createNativeQuery($sql,$rsm)->getArrayResult();
    }

这是我的selectmissionwithtags,它返回空数组。
enter image description here
我没有GROUP_CONCAT,所以我不能使用这种查询生成器。NativeQuery -〉我暂时使用这种方法。
我能做的最后一件事是为一个数据库视图创建实体,并创建相关的实体。但是我现在想了解一下querynative方法。
非常感谢;)

eagi6jfj

eagi6jfj1#

ResultSetMapping**需要非常显式才能工作,您必须执行以下操作

$rsm->addEntityResult(Mission::class, 'mission');
    $rsm->addFieldResult('mission', 'id', "id");
    //same for all fields
    $rsm->addJoinedEntityResult(MissionTag::class, 'mt', 'mission', 'mission_tag');
    $rsm->addFieldResult('mt', 'id', 'id);
    //same for all fields of related entities

但是,ResultSetMappingBuilder可以提供帮助

$rsmb->addRootEntityFromClassMetadata(Mission::class);

这就是你所要做的,用教义来处理关系和水合。它将尝试从你的SELECT中水合,所以你需要在查询的SELECT中使用水合器中使用的所有字段。
可使用以下命令指定连接关系:

$rsm->addJoinedEntityFromClassMetadata(MissionTag::class, 'mt', 'mission', 'relationName', array('id' => "tag_id"));

但是,您需要在查询中使用连接(它将在select中查找关系),并且您需要注意何时使用原则名称(relationName),何时使用数据库名称(tag_id
您还可以通过利用Doctrine的用户定义函数,将GROUP_CONCAT添加到Doctrine中来存档您的目的
我已经找到了一个snippet做正是这样,它不是我的,我还没有测试它。

相关问题