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?
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()