Hyperion
Hyperion

Reputation: 308

Only POST elements with value=true

I'm using Api-platform as a backend for a project. It sends data from a class called "Voiture". But I don't need all elements of "Voiture", this class has an boolean element called Parked.

And I only want to send the elements where Parked= true. Is it possible to do this, I don't think it would be practical to filter the data from the receiving side.

this is my "Voiture" class:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;


/**
 * @ApiResource(
 *      attributes={"order"={"gareele": "DESC"}}
 * )
 * @ORM\Entity(repositoryClass="App\Repository\VoitureRepository")
 * @ApiFilter(
 *      SearchFilter::class,
 *      properties={
 *          "matricule": "partial"
 *      }
 * )
 */
class Voiture
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"toute"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=200)
     * @Groups({"toute"})
     */
    private $matricule;


    /**
     * @ORM\Column(type="boolean")
     * @Groups({"toute"})
     */
    private $parked;



    public function getId(): ?int
    {
        return $this->id;
    }

    public function getMatricule(): ?string
    {
        return $this->matricule;
    }

    public function setMatricule(string $matricule): self
    {
        $this->matricule = $matricule;

        return $this;
    }

    public function getParked(): ?bool
    {
        return $this->parked;
    }

    public function setParked(bool $parked): self
    {
        $this->parked = $parked;

        return $this;
    }


}

Upvotes: 0

Views: 178

Answers (3)

Hyperion
Hyperion

Reputation: 308

So I finally found a solution, turns out there's an annotation for Api Platform that filters Boolean data. I just added the annotation below to my class and it worked when I add ?parked=true to my address and it works fine.

 * @ApiFilter(BooleanFilter::class,
 *  properties={
 * "parked"
 * }
 * )

Upvotes: 1

Ricardo Jesus Ruiz
Ricardo Jesus Ruiz

Reputation: 135

Maybe you are looking for annotations personalized, im using api-platform to, especifically GraphQL, so, i dont need all the rows, because, i have a logical delelte, no phisical delete. instead of make a bussiness rule, and any developer make his own interpretation of it, we make a personal annotation.

See this link, this link also apply on SF4.

Upvotes: 0

Arleigh Hix
Arleigh Hix

Reputation: 10897

Yes, the repository has methods to find entities by any criteria. You will want something like the following in your controller:

use App\Entity\Voiture;
...

  $repository = $this->getDoctrine()->getRepository(Voiture::class);

  $parkedVoitures = $repository->findBy(
    ['parked' => true]
  );
  $parkedVoituresOrderedByMatricule = $repository->findBy(
    ['parked' => true],
    ['matricule' => 'ASC']
  );

https://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database

Upvotes: 0

Related Questions