【问题标题】:Nuxtjs + Symfony ApiPlatform POST or PUT errorNuxtjs + Symfony ApiPlatform POST 或 PUT 错误
【发布时间】:2021-07-31 23:51:33
【问题描述】:

我在尝试发布或放置 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", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "ParticipantWrite",
     *     "TeamCollection", "TeamWrite", "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", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "ParticipantWrite",
     *     "TeamCollection", "TeamWrite", "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", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "ParticipantWrite",
     *     "TeamCollection", "TeamWrite", "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", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "ParticipantWrite",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $genderAllowed;

    /**
     * @ORM\Column(type="date", nullable=true)
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "ParticipantWrite",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $date;

    /**
     * @ORM\Column(type="time", nullable=true)
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "ParticipantWrite",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $time;

    /**
     * @ORM\OneToMany(targetEntity=RaceTeam::class, mappedBy="race", orphanRemoval=true, cascade={"persist"})
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "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"}, "enable_max_depth"=true},
 *         "denormalization_context"={"groups"={"write"}}
 *     }
 * )
 * @ORM\Entity(repositoryClass=RaceTeamRepository::class)
 */
class RaceTeam
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $id;

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

    /**
     * @ORM\ManyToOne(targetEntity=Team::class, inversedBy="raceTeams", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem"
     * })
     */
    private $team;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $timeStart;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $timeEnd;

    /**
     * @ORM\Column(type="time", nullable=true)
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $timePenalty;

    /**
     * @ORM\Column(type="boolean")
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "TeamCollection", "TeamWrite", "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"}, "enable_max_depth"=true},
 *         "denormalization_context"={"groups"={"write"}}
 *     },
 *     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', 'H', 'M'];
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceWrite", "RaceItem",
     *     "ParticipantCollection", "ParticipantWrite", "ParticipantItem",
     *     "TeamCollection", "TeamWrite", "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", "write",
     *     "RaceCollection", "RaceItem", "RaceWrite",
     *     "ParticipantCollection", "ParticipantWrite", "ParticipantItem",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=1)
     * @Assert\Choice(choices=Team::GENRES, message="La catégorie de l'équipe doit être Mixte, Hommes ou Femmes")
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceItem", "RaceWrite",
     *     "ParticipantCollection", "ParticipantWrite", "ParticipantItem",
     *     "TeamCollection", "TeamWrite", "TeamItem"
     * })
     */
    private $genderAllowed;

    /**
     * @ORM\ManyToMany(targetEntity=Participant::class, inversedBy="teams")
     * @Groups({"read", "write",
     *     "RaceCollection", "RaceItem",
     *     "TeamCollection", "TeamWrite", "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", "write",
     *     "TeamCollection", "TeamWrite", "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

【问题讨论】:

    标签: php symfony doctrine nuxt.js api-platform.com


    【解决方案1】:

    您没有在控制台中看到错误吗?您的memory_limit 设置使用了很多内存。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 2020-05-09
      • 2010-12-10
      • 1970-01-01
      相关资源
      最近更新 更多