微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony 自定义方法存储库无法在控制器中访问

如何解决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();

    }

我的实体类看起来像

<?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-container 中默认启用)并重写存储库,以便它不会扩展任何基本存储库。相反,将 EntityManager 注入 Repository 构造函数,然后将此具体存储库注入控制器操作。如果您输入提示参数,symfony 自动装配将完成所有其他工作。

详情请见此处https://tomasvotruba.com/blog/2017/10/16/how-to-use-repository-with-doctrine-as-service-in-symfony/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?