Symfony 自定义方法存储库无法在控制器中访问
Symfony custom method repository not able to access in controller
我在 symfony 中有两个数据库连接。我在存储库中创建了一个方法,并试图在控制器中访问该方法。但是,我无法访问该方法,并且出现错误(未定义的方法 getSuitelog())请告诉我如何从控制器中的存储库访问查询和方法。
<?php
namespace App\Controller\Testing;
use App\Entity\Testing\CpSelection;
use App\Entity\Testing\CpTests;
use App\Repository\CpSelectionRepository;
use App\Repository\CpTestsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class TestingController extends AbstractController
{
#[Route('/testing/id/{{id}}', name: 'index')]
public function CpTestSuit($id): Response
{
// $entityManager = $this->get('doctrine.orm.testing_entity_manager');
$repository = $this->getDoctrine()
->getManager('testing');
$cpsuit = $repository->getRepository(CpSelection::class, 'CpSelection')
->getSuitelog($id);
$cpTests = $repository->getRepository(CpTests::class, 'CpTests')
->findAll(); //
return $this->render('testing/index.html.twig', [
'controller_name' => 'TestingController',
'suite' => $cpsuit,
'tests' => $cpTests,
]);
}
使用 getSuitelog 方法的存储库:
<?php
namespace App\Repository;
use App\Entity\Testing\CpSelection;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method CpSelection|null find($id, $lockMode = null, $lockVersion = null)
* @method CpSelection|null findOneBy(array $criteria, array $orderBy = null)
* @method CpSelection[] findAll()
* @method CpSelection[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CpSelectionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CpSelection::class);
}
public function getSuitelog($suite_id = NULL) {
$where = "";
$values = array ();
if ($suite_id != NULL) {
$where = " where id = ? ";
$values = array (1 => $suite_id);
}
$query = "SELECT * FROM App:CpSelection" . $where;
return $this->getEntityManager()
->createQuery(
$query
)
->execute()
->getArrayResult();
}
我的实体class看起来像
<?php
namespace App\Entity\Testing;
use Doctrine\ORM\Mapping as ORM;
/**
* CpSelection
* @ORM\Entity(repositoryClass="App\Repository\CpSelectionRepository")
* @ORM\Table(name="cp_selection")
*/
class CpSelection
{
......
}
自定义存储库注入示例:
#[Route('/testing/id/{{id}}', name: 'index')]
public function CpTestSuit($id, CpSelectionRepository $myCustomRepository): Response
{
...
}
更新:
为了完成这项工作,我们应该使用自动装配(在 symfony DI 容器中默认启用)并重写存储库,这样它就不会扩展任何基本存储库。相反,将 EntityManager 注入到 Repository 构造函数中,然后将这个具体的存储库注入到控制器操作中。如果您输入提示参数,symfony 自动装配将完成所有其他工作。
这里有详细说明https://tomasvotruba.com/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/
我在 symfony 中有两个数据库连接。我在存储库中创建了一个方法,并试图在控制器中访问该方法。但是,我无法访问该方法,并且出现错误(未定义的方法 getSuitelog())请告诉我如何从控制器中的存储库访问查询和方法。
<?php
namespace App\Controller\Testing;
use App\Entity\Testing\CpSelection;
use App\Entity\Testing\CpTests;
use App\Repository\CpSelectionRepository;
use App\Repository\CpTestsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class TestingController extends AbstractController
{
#[Route('/testing/id/{{id}}', name: 'index')]
public function CpTestSuit($id): Response
{
// $entityManager = $this->get('doctrine.orm.testing_entity_manager');
$repository = $this->getDoctrine()
->getManager('testing');
$cpsuit = $repository->getRepository(CpSelection::class, 'CpSelection')
->getSuitelog($id);
$cpTests = $repository->getRepository(CpTests::class, 'CpTests')
->findAll(); //
return $this->render('testing/index.html.twig', [
'controller_name' => 'TestingController',
'suite' => $cpsuit,
'tests' => $cpTests,
]);
}
使用 getSuitelog 方法的存储库:
<?php
namespace App\Repository;
use App\Entity\Testing\CpSelection;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method CpSelection|null find($id, $lockMode = null, $lockVersion = null)
* @method CpSelection|null findOneBy(array $criteria, array $orderBy = null)
* @method CpSelection[] findAll()
* @method CpSelection[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CpSelectionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CpSelection::class);
}
public function getSuitelog($suite_id = NULL) {
$where = "";
$values = array ();
if ($suite_id != NULL) {
$where = " where id = ? ";
$values = array (1 => $suite_id);
}
$query = "SELECT * FROM App:CpSelection" . $where;
return $this->getEntityManager()
->createQuery(
$query
)
->execute()
->getArrayResult();
}
我的实体class看起来像
<?php
namespace App\Entity\Testing;
use Doctrine\ORM\Mapping as ORM;
/**
* CpSelection
* @ORM\Entity(repositoryClass="App\Repository\CpSelectionRepository")
* @ORM\Table(name="cp_selection")
*/
class CpSelection
{
......
}
自定义存储库注入示例:
#[Route('/testing/id/{{id}}', name: 'index')]
public function CpTestSuit($id, CpSelectionRepository $myCustomRepository): Response
{
...
}
更新: 为了完成这项工作,我们应该使用自动装配(在 symfony DI 容器中默认启用)并重写存储库,这样它就不会扩展任何基本存储库。相反,将 EntityManager 注入到 Repository 构造函数中,然后将这个具体的存储库注入到控制器操作中。如果您输入提示参数,symfony 自动装配将完成所有其他工作。
这里有详细说明https://tomasvotruba.com/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/