donglunzai4288 2018-07-10 08:43
浏览 30

外部存储库中的Symfony EntityManager

I need some help.

I have a DomainController which I use to define some Propertys and create a Domain Entity

public function new(Request $request): Response{
        $data = json_decode($request->getContent(), true);
        $domain = new Domain();
        $domain->setUri($data['record']['uri']);
        $em = $this->getDoctrine()->getManager();
        $em->persist($domain);
        $em->flush($domain);

        $id = $domain->getId();
        $uri = $domain->getUri();

          $businessRepository = new BusinessRepository();
          $result = $businessRepository->newDomainCheck($id, $uri);
        return new JsonResponse($result);
}

You can see, that I call my BusinessRepository->newDomainCheck($id, $uri)

Problem:

My $id variable is empty, but i need this int after flushing.

public function newDomainCheck($id, $uri){
        var_dump($id);
        var_dump($uri);

        $domain = new Domain();
        $domain->setUri($uri);
        $domain->setId($id);
        $result = $this->analyzeDomain($uri);
        $res = json_decode($result->getContent(), true);
        $domainCheckResult = $this->createResponse($res, $domain);
        $em = $this
            ->getEntityManager()
            ->getRepository('App:DomainCheckResult');
        $em->persist($domainCheckResult);
        $em->flush($domainCheckResult);
        return new JsonResponse($domainCheckResult);
}

Problem:

Attempted to call undefined method named "getEntityManager" of class "App\Repository\BusinessRepository"

Can you help me, to solve this?

  • 写回答

1条回答 默认 最新

  • douqiu9529 2018-07-10 08:55
    关注

    Make sure that your Repository is properly registered in your Business entity

    If you use annotations, it is something like

    @ORM\Entity(repositoryClass="AppBundle\Repository\BusinessRepository")

    And instead of instantiating it with $businessRepository = new BusinessRepository();

    you should get it from the entity manager with

    $this->getEntityManager()->getRepository(Business::class);

    If there is no entity, you should treat it as a Service instead and inject the Entity Manager to it.

    Also, in your newDomainCheck you do not flush using the entity manager, but the App:DomainCheckResult repository.

    Your $em getter should stop at

    ->getEntityManager()

    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错