Reputation: 85
i've tried to make controllers for my symfony API, however using the autowire feature of symfony keeps returning nullthe code returns null when i use the function $trustedUser->getName()
the code returns null when i use the function: $trustedUser->getName() In the Controller at getTrustedUserByID()
$this->logger->info($trustedUser->getName()." ".$trustedUser->getId());
return new JsonResponse([
"name" => $trustedUser->getName(),
]);
my controller looks like this:
<?php
namespace App\Controller;
use App\Entity\Status;
use App\Entity\TrustedUser;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Psr\Log\LoggerInterface;
class TrustedUserController extends AbstractController
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @Route("/trustedUser/{id}", name="getTrustedUserByID", methods={"GET","HEAD"})
*/
public function getTrustedUserByID(TrustedUser $trustedUser,$id)
{
if (!$trustedUser) {
throw $this->createNotFoundException(
'No trustedUsers found for id '.$id
);
}
$this->logger->info($trustedUser->getName()." ".$trustedUser->getId());
return new JsonResponse([
"name" => $trustedUser->getName(),
]);
}
}
My entity like this:
<?php
namespace App\Entity;
use App\Repository\TrustedUserRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TrustedUserRepository::class)
*/
class TrustedUser
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $civilite;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastname;
/**
* @ORM\Column(type="integer")
*/
private $phone;
/**
* @ORM\Column(type="string", length=255)
*/
private $mail;
/**
* @ORM\OneToOne(targetEntity=Status::class, inversedBy="trustedUser", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $id_status;
public function getId(): ?int
{
return $this->id;
}
public function getCivilite(): ?string
{
return $this->civilite;
}
public function setCivilite(string $civilite): self
{
$this->civilite = $civilite;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getPhone(): ?int
{
return $this->phone;
}
public function setPhone(int $phone): self
{
$this->phone = $phone;
return $this;
}
public function getMail(): ?string
{
return $this->mail;
}
public function setMail(string $mail): self
{
$this->mail = $mail;
return $this;
}
public function getIdStatus(): ?Status
{
return $this->id_status;
}
public function setIdStatus(Status $id_status): self
{
$this->id_status = $id_status;
return $this;
}
}
And my repository looks like this:
<?php
namespace App\Repository;
use App\Entity\TrustedUser;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method TrustedUser|null find($id, $lockMode = null, $lockVersion = null)
* @method TrustedUser|null findOneBy(array $criteria, array $orderBy = null)
* @method TrustedUser[] findAll()
* @method TrustedUser[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TrustedUserRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TrustedUser::class);
}
// /**
// * @return TrustedUser[] Returns an array of TrustedUser objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->orderBy('t.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?TrustedUser
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
Both the entity and repository were generated using the make:entity
And the database looks like this The results looks like this
Upvotes: 1
Views: 191
Reputation: 12094
This does not look like an autowiring issue to me - Doctrine entities are not autowired, but loaded through a ParamConverter
. This enables injecting an entity to a controller method through providing the ID of that entity.
To get this working, you need to install the package sensio/framework-extra-bundle
. This package contains the converter.
Additionally, if you don't need the raw ID in your controller, there's no need to inject it. The package's converter will throw an exception on its own if the entity is not found
Upvotes: 2