微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nuxtjs + Symfony ApiPlatform POST 或 PUT 错误

如何解决Nuxtjs + Symfony ApiPlatform POST 或 PUT 错误

我在尝试发布或放置 Race 时出错,这是我的类和发送到 api 平台的标头,请告诉我您是否还需要其他内容

我迷路了,请帮忙:'(

比赛

<?PHP

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\datefilter;
use ApiPlatform\Core\Api\FilterInterface;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\RaceRepository;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(
 *     attributes={
 *         "normalization_context"={"groups"={"read"},"enable_max_depth"=true},*         "denormalization_context"={"groups"={"write"}}
 *     },*     collectionoperations={
 *         "get"={
 *             "normalization_context"={"groups"={"RaceCollection"}}
 *         },*         "post"={
 *             "denormalization_context"={"groups"={"RaceWrite"},"enable_max_depth"=true}
 *         }
 *     },*     itemOperations={
 *         "get"={
 *             "normalization_context"={"groups"={"RaceItem"}}
 *         },*         "put"={
 *             "denormalization_context"={"groups"={"RaceWrite"},"enable_max_depth"=true}
 *         },*         "delete"={
 *             "normalization_context"={"groups"={"RaceItemDelete"}}
 *         }
 *     }
 * )
 * @ORM\Entity(repositoryClass=RaceRepository::class)
 */
class Race
{
    const GENRES = ['F','H','M'];
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"read","write",*     "RaceCollection","RaceWrite","RaceItem","RaceItemDelete",*     "ParticipantCollection","ParticipantWrite",*     "TeamCollection","TeamWrite","TeamItem"
     * })
     */
    private $id;

    /**
     * @ORM\Column(type="string",length=70)
     * @Assert\NotBlank(message="Le nom de la course est obligatoire")
     * @Assert\Length(max=70,maxMessage="Le nom de la course ne doit pas dépasser 70 caractères")
     * @Groups({"read","TeamItem"
     * })
     */
    private $name;

    /**
     * @ORM\Column(type="decimal",precision=5,scale=2,nullable=true)
     * @Assert\NotBlank(message="La distance de la course est obligatoire")
     * @Assert\Range(min="0.01",max="999.99",notinRangeMessage="La distance de la course doit être entre 0.01km et 999.99km")
     * @Groups({"read","TeamItem"
     * })
     */
    private $distance;

    /**
     * @ORM\Column(type="integer",nullable=true)
     * @Assert\NotBlank(message="Le nombre d'équipes maximum de la course est obligatoire")
     * @Assert\GreaterThan(value="0",message="Au moins une équipe doit pouvoir être inscrite")
     * @Groups({"read","TeamItem"
     * })
     */
    private $maxTeams;

    /**
     * @ORM\Column(type="string",length=1)
     * @Assert\Choice(choices=Race::GENRES,message="La catégorie de la course doit être Mixte,Hommes ou Femmes")
     * @Groups({"read","TeamItem"
     * })
     */
    private $genderAllowed;

    /**
     * @ORM\Column(type="date",nullable=true)
     * @Groups({"read","TeamItem"
     * })
     */
    private $date;

    /**
     * @ORM\Column(type="time","TeamItem"
     * })
     */
    private $time;

    /**
     * @ORM\OnetoMany(targetEntity=RaceTeam::class,mappedBy="race",orphanRemoval=true,cascade={"persist"})
     * @Groups({"read","ParticipantWrite"
     * })
     * @Assert\Count(min=1,minMessage="Au moins une équipe doit être inscrite")
     */
    private $raceTeams;

    public function __construct()
    {
        $this->raceTeams = new ArrayCollection();
    }

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getMaxTeams(): ?int
    {
        return $this->maxTeams;
    }

    public function setMaxTeams(?int $maxTeams): self
    {
        $this->maxTeams = $maxTeams;

        return $this;
    }

    public function getGenderAllowed(): ?string
    {
        return $this->genderAllowed;
    }

    public function setGenderAllowed(string $genderAllowed): self
    {
        $this->genderAllowed = $genderAllowed;

        return $this;
    }

    public function getDate(): ?DateTimeInterface
    {
        return $this->date;
    }

    public function setDate(?DateTimeInterface $date): self
    {
        $this->date = $date;

        return $this;
    }

    public function getTime(): ?DateTimeInterface
    {
        return $this->time;
    }

    public function setTime(?DateTimeInterface $time): self
    {
        $this->time = $time;

        return $this;
    }

    public function getdistance(): ?string
    {
        return $this->distance;
    }

    public function setdistance(?string $distance): self
    {
        $this->distance = $distance;

        return $this;
    }

    /**
     * @return Collection|RaceTeam[]
     */
    public function getRaceTeams(): Collection
    {
        return $this->raceTeams;
    }

    public function addRaceTeam(RaceTeam $raceTeam): self
    {
        if (!$this->raceTeams->contains($raceTeam)) {
            $this->raceTeams[] = $raceTeam;
            $raceTeam->setRace($this);
        }

        return $this;
    }

    public function removeRaceTeam(RaceTeam $raceTeam): self
    {
        if ($this->raceTeams->removeElement($raceTeam)) {
            // set the owning side to null (unless already changed)
            if ($raceTeam->getRace() === $this) {
                $raceTeam->setRace(null);
            }
        }

        return $this;
    }
}

赛车队

<?PHP

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\RaceTeamRepository;
use DateTime;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource(
 *     attributes={
 *         "normalization_context"={"groups"={"read"},*         "denormalization_context"={"groups"={"write"}}
 *     }
 * )
 * @ORM\Entity(repositoryClass=RaceTeamRepository::class)
 */
class RaceTeam
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"read","TeamItem"
     * })
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity=Race::class,inversedBy="raceTeams",cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"read","TeamItem"
     * })
     */
    private $race;

    /**
     * @ORM\ManyToOne(targetEntity=Team::class,"RaceItem"
     * })
     */
    private $team;

    /**
     * @ORM\Column(type="datetime","TeamItem"
     * })
     */
    private $timeStart;

    /**
     * @ORM\Column(type="datetime","TeamItem"
     * })
     */
    private $timeEnd;

    /**
     * @ORM\Column(type="time","TeamItem"
     * })
     */
    private $timePenalty;

    /**
     * @ORM\Column(type="boolean")
     * @Groups({"read","TeamItem"
     * })
     */
    private $havePaid = false;

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

    public function getRace(): ?Race
    {
        return $this->race;
    }

    public function setRace(?Race $race): self
    {
        $this->race = $race;

        return $this;
    }

    public function getTeam(): ?Team
    {
        return $this->team;
    }

    public function setTeam(?Team $team): self
    {
        $this->team = $team;

        return $this;
    }

    public function getTimeStart(): ?DateTimeInterface
    {
        return $this->timeStart;
    }

    public function setTimeStart(?DateTimeInterface $timeStart): self
    {
        $this->timeStart = $timeStart;

        return $this;
    }

    public function getTimeEnd(): ?DateTimeInterface
    {
        return $this->timeEnd;
    }

    public function setTimeEnd(?DateTimeInterface $timeEnd): self
    {
        $this->timeEnd = $timeEnd;

        return $this;
    }

    public function getTimePenalty(): ?DateTimeInterface
    {
        return $this->timePenalty;
    }

    public function setTimePenalty(?DateTimeInterface $timePenalty): self
    {
        $this->timePenalty = $timePenalty;

        return $this;
    }

    public function getHavePaid(): ?bool
    {
        return $this->havePaid;
    }

    public function setHavePaid(bool $havePaid): self
    {
        $this->havePaid = $havePaid;

        return $this;
    }
}

团队

<?PHP

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\TeamRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(
 *     attributes={
 *         "normalization_context"={"groups"={"read"},*     collectionoperations={
 *         "get"={
 *             "normalization_context"={"groups"={"TeamCollection"}}
 *         },*         "post"={
 *             "denormalization_context"={"groups"={"TeamWrite"}}
 *         }
 *     },*     itemOperations={
 *         "get"={
 *             "normalization_context"={"groups"={"TeamItem"}}
 *         },*         "put"={
 *             "normalization_context"={"groups"={"TeamWrite"}}
 *         },*         "delete"={
 *             "normalization_context"={"groups"={"TeamDelete"}}
 *         }
 *     }
 * )
 * @ORM\Entity(repositoryClass=TeamRepository::class)
 * @UniqueEntity(fields={"name"},message="Cette équipe existe déjà")
 */
class Team
{
    const GENRES = ['F',"ParticipantItem","TeamItem","TeamDelete"
     * })
     */
    private $id;

    /**
     * @ORM\Column(type="string",length=40,unique=true)
     * @Assert\NotBlank(message="Le nom de l'équipe est obligatoire")
     * @Assert\Length(max=40,maxMessage="Le nom de l'équipe ne doit pas dépasser 40 caractères")
     * @Groups({"read","TeamItem"
     * })
     */
    private $name;

    /**
     * @ORM\Column(type="string",length=1)
     * @Assert\Choice(choices=Team::GENRES,message="La catégorie de l'équipe doit être Mixte,"TeamItem"
     * })
     */
    private $genderAllowed;

    /**
     * @ORM\ManyToMany(targetEntity=Participant::class,inversedBy="teams")
     * @Groups({"read","TeamItem"
     * })
     * @Assert\Count(min=1,minMessage="Au moins un participant doit être dans l'équipe")
     */
    private $participants;

    /**
     * @ORM\OnetoMany(targetEntity=RaceTeam::class,mappedBy="team",orphanRemoval=true)
     * @Groups({"read","TeamItem"
     * })
     */
    private $raceTeams;

    public function __construct()
    {
        $this->participants = new ArrayCollection();
        $this->raceTeams = new ArrayCollection();
    }

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return Collection|Participant[]
     */
    public function getParticipants(): Collection
    {
        return $this->participants;
    }

    public function addParticipant(Participant $participant): self
    {
        if (!$this->participants->contains($participant)) {
            $this->participants[] = $participant;
        }

        return $this;
    }

    public function removeParticipant(Participant $participant): self
    {
        $this->participants->removeElement($participant);

        return $this;
    }

    public function getGenderAllowed(): ?string
    {
        return $this->genderAllowed;
    }

    public function setGenderAllowed(string $genderAllowed): self
    {
        $this->genderAllowed = $genderAllowed;

        return $this;
    }

    /**
     * @return Collection|RaceTeam[]
     */
    public function getRaceTeams(): Collection
    {
        return $this->raceTeams;
    }

    public function addRaceTeam(RaceTeam $raceTeam): self
    {
        if (!$this->raceTeams->contains($raceTeam)) {
            $this->raceTeams[] = $raceTeam;
            $raceTeam->setTeam($this);
        }

        return $this;
    }

    public function removeRaceTeam(RaceTeam $raceTeam): self
    {
        if ($this->raceTeams->removeElement($raceTeam)) {
            // set the owning side to null (unless already changed)
            if ($raceTeam->getTeam() === $this) {
                $raceTeam->setTeam(null);
            }
        }

        return $this;
    }
}

发送的对象:

{
    "name": "testPost","distance": "7.5","maxTeams": 50,"genderAllowed": "M","date": "2021-08-01","time": "12:15","raceTeams": [
        {
            "team": {
                "@id": "/api/teams/156","@type": "Team","id": 156,"name": "les Frelons Formidables","genderAllowed": "M"
            },"timeStart": "1970-01-01 00:00:00","timeEnd": null,"havePaid": false
        }
    ]
}

控制台中的错误: 我可以看到 AbstractObjectnormalizer,但我看不到它的来源 errorinconsole

解决方法

您没有在控制台中看到错误吗?您的 memory_limit 设置占用了大量内存。

https://haydenjames.io/understanding-php-memory_limit/https://www.php.net/manual/en/ini.core.php

您可以尝试增加限制 - 但它更可能是循环序列化的问题。 (对于涉及的许多实体 - 如果涉及许多行/实体,序列化/反序列化需要大量内存。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。