Home > OS >  Symfony - Request data as integer to relational database
Symfony - Request data as integer to relational database

Time:01-26

I have created a project that have three tables (hardwarePlacement , HardwareUnitType, hardwareUnit)

And created the entities / controllers for them, with get, post, put and delete. And it works perfectly when i test the methods for hardwarePlacement and HardwareUnitType, but the last table "hardwareUnit" is a relational table to the other two. so i have Forign keys (hardwarePlacementId and HardwareUnitTypeId).

So when i from postman try to make a post request, i get the error: "that my setHardwareUnitTypeId and hardwarePlacementId must be of type integer".

In my HardwareUnit entity i have the following for the other tables:

#[ORM\ManyToOne(inversedBy: 'hardwareUnits')]
#[ORM\JoinColumn(nullable: false)]
private ?HardwareUnitType $hardwareUnitTypeId = null;

#[ORM\ManyToOne(inversedBy: 'hardwareUnits')]
#[ORM\JoinColumn(nullable: false)]
private ?HardwarePlacement $hardwarePlacementId = null;

public function getHardwareUnitTypeId(): ?HardwareUnitType
{
    return $this->hardwareUnitTypeId;
}

public function setHardwareUnitTypeId(?HardwareUnitType $hardwareUnitTypeId): self
{
    $this->hardwareUnitTypeId = $hardwareUnitTypeId;

    return $this;
}

public function getHardwarePlacementId(): ?HardwarePlacement
{
    return $this->hardwarePlacementId;
}

public function setHardwarePlacementId(?HardwarePlacement $hardwarePlacementId): self
{
    $this->hardwarePlacementId = $hardwarePlacementId;

    return $this;
}

And my create method in HardwareUnit controller:

#[Route('/hardwareUnit', name: 'hardwareUnit_new', methods: ['POST'])]
public function new(ManagerRegistry $doctrine, Request $request): JsonResponse
{
    $entityManager = $doctrine->getManager();

    $hardwareUnit = new HardwareUnit();
    $hardwareUnit->setHardwareUnitTypeId($request->request->get('hardwareUnitTypeId'));
    $hardwareUnit->setHardwarePlacementId($request->request->get('hardwarePlacementId'));
    $hardwareUnit->setName($request->request->get('name'));
    $hardwareUnit->setCreatedDate(new \DateTime());
    $hardwareUnit->setEditedDate(new \DateTime());

    $entityManager->persist($hardwareUnit);
    $entityManager->flush();

    return $this->json('Oprettet ny hardware unit id: ' . $hardwareUnit->getId());
}

I have tried retrieving request as intval:

$hardwareUnit->setHardwareUnitTypeId($request->request->get(intval('hardwareUnitTypeId')));
$hardwareUnit->setHardwarePlacementId($request->request->get(intval('hardwarePlacementId')));

But then i get the error that my post value for setHardwareUnitTypeId and setHardwarePlacementId is null

Any suggestions on how i can convert my request to int?

Here is an image of my postman, if it helps: enter image description here

CodePudding user response:

You're reading the error wrong. It states that the argument of setHardwareUnitTypeId should be of type HardwareUnitType, but that you're providing a string:

App\Entity\HardwareUnit::setHardwareUnitTypeId(): Argument #1 ($hardwareUnitTypeId) must be of type ?App\Entity\HardwareUnitType, string given ...

Looking at your code, the error is quite clear. In your "new" route, you're calling the functions like this:

$hardwareUnit->setHardwareUnitTypeId($request->request->get('hardwareUnitTypeId'));
$hardwareUnit->setHardwarePlacementId($request->request->get('hardwarePlacementId'));

The $request->request->get() method returns a string (or int|float|bool|null), as it's parsing request parameters. It's not "magically" returning objects of the correct type. You need to take that ID and fetch the correct entity.

So what you need to do is the following:

  1. Get the repository for HardwareUnitType and HardwarePlacement. This can be done through dependency injection of the repository directly into your controller action "new". See the docs for more info. You could also use $entityManager that you already have, like this example from docs, to get the repository.
  2. In your controller, use the repository to fetch the entity based on $request->request->get('hardwareUnitTypeId') and $request->request->get('hardwarePlacementId'). You should be able to use the repository's built in find method (see previous example from docs). You'd get something like this:
$entity = $repository->find((int) $request->request->get('...'));
  1. Use the result from find as argument to your setters. Building on on the example at the previous list item: you'd get something like: $hardwareUnit->setHardwareUnitTypeId($entity)

I hope this will help you figuring out your problem. Let me know if you need more help!

  • Related