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

如何在 symfony 中使用 api-platform 来编辑帖子数据?

如何解决如何在 symfony 中使用 api-platform 来编辑帖子数据?

我是 symfony 的新手,但主要是 api 平台。所以我查看了 api-platform 以了解更多信息。我读了很大部分,信息量很大。但我只是找不到一种方法来使用 openAPI 来获取编辑帖子数据。我也在使用学说和 sqlite

所以我所说的编辑帖子数据是什么意思。我安装了 api-platform 并使其正常工作(看起来像 this)。我可以在 url www.localhost/api 上使用我的 api-platform POST 数据,然后在 Role 下选择 POST,然后“try out”。 我在学说中做了一个小表,所以我发送给它的 JSON 是;

{
  "role": "string"
}

这样就可以了,它将数据添加到我的 sqlite 数据库中。但我想在 PHP 中编辑这些数据。但我就是不知道如何检索这个 JSON 并编辑我从 JSON 中获得的数据。

我所拥有的是;名为 Role 和 RoleRepository 的实体和存储库。我读过一个控制器不需要使用 api-platform 编辑数据。但是我在任何地方都找不到如何编辑数据。

那么如何编辑从 JSON 中获得的数据?

编辑: 如果你想知道我的实体角色是什么样子的;

<?PHP
namespace App\Entity;

use App\Repository\RoleRepository;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=RoleRepository::class)
 */
class Role
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string",length=255)
     */
    private $role;

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

    public function getRole(): ?string
    {
        return $this->role;
    }

    public function setRole(string $role): self
    {
        $this->role = $role;

        return $this;
    }
}

和RoleRepository

<?PHP

namespace App\Repository;

use App\Entity\Role;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method Role|null find($id,$lockMode = null,$lockVersion = null)
 * @method Role|null findOneBy(array $criteria,array $orderBy = null)
 * @method Role[]    findAll()
 * @method Role[]    findBy(array $criteria,array $orderBy = null,$limit = null,$offset = null)
 */
class RoleRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry,Role::class);
    }

    // /**
    //  * @return Role[] Returns an array of Role objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createqueryBuilder('r')
            ->andWhere('r.exampleField = :val')
            ->setParameter('val',$value)
            ->orderBy('r.id','ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Role
    {
        return $this->createqueryBuilder('r')
            ->andWhere('r.exampleField = :val')
            ->setParameter('val',$value)
            ->getQuery()
            ->getoneOrNullResult()
        ;
    }
    */
}

解决方法

Symfony 提供了所谓的内核事件,开发人员可以将其挂钩以直接处理请求/响应,而无需编写特定的控制器或在数据到达控制器之前添加属性或更改某些内容。你可以找到这个in the docs for the HttpKernel component

API 平台通过挂钩这些事件来工作。您可以通过使用事件调度程序的 debug 命令来查看这一点:

bin/console debug:event-dispatcher

这应该打印出一个事件列表以及对事件起作用的侦听器,并且使用 api-platform 你应该有一些新的。或者,您可以查找 the list of provided event listeners in the api-platform docs。对您而言,ReadListener 和 WriteListener 可能是最相关的。您还可以查看 DeserializeListener,但这适用于所有 json 输入,因此您必须小心。

ReadListener 将使用 data providers 从数据库中读取现有数据。当您想要编辑它时,您可以从这里获取现有实体。 WriteListener 是将新的或编辑过的数据写入数据库的方式。这使用 data persisters

如果您想手动更改编辑实体的工作方式,您可能需要编写自己的 custom data persistercustom data provider

当您编写自己的数据持久化程序时,您需要确保它支持您的实体。您几乎可以从文档中复制示例,只需更改类名即可。然后 api-platform 将调用您的持久化程序,您可以使用 var_dump 或 xdebug 检查您在 persist 方法中获得的值。第一个参数 $data 应该是您的实体,在第二个参数 $context 中您应该获得更多信息,例如使用了哪个 http 动词,我认为也是解码的 json 数据。

编写自己的提供程序和持久程序可能有点棘手,通常不需要为每个实体都这样做(除非您不想在它们上使用标准的 getter 和 setter 方法)。相反,您还可以通过添加类似于 @ApiResource 注释的元数据信息来修改实体的处理方式,例如用于验证或更改类的(反)序列化方式。此外,持久器适用于已修改的实体,因此您的 json 数据已经过验证。没有什么能阻止您在那里进行额外的验证,但它可能是多余的,或者您可能认为为时已晚。简而言之,为了验证,您应该编写 your own validation constraints 或使用 Symfony 提供的那些。例如,在 isX/hasX/getX 方法上使用 IsTrue constraint 之类的东西可能是解决验证需求的好方法。

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