给定的api-platform字符串,必须是实体的类型

发布于 2025-02-13 16:40:46 字数 1038 浏览 3 评论 0原文

我猜想简单的问题,但是我无法使其正常工作:

im遇到此错误:app \ entity \ commands :: setServerId():参数#1($ serverid)必须是类型?app \ entity \ entity \ entity \ entity \ entity \ entity \ entity服务器,字符串给定 当我尝试通过api-platform添加新元素时,其工作正常工作:

”在此处输入图像描述

,这是结果:

但是当我尝试通过邮递员或在我的控制器中进行Symfony Im在开始中提到错误时:

        $commandDB = new Commands();
        $commandDB->setCommand($command);
        $commandDB->setStatusCode(1);
        $commandDB->setOutput("added to DB");
        $commandDB->setCreatedAt($date);
        $commandDB->setServerID($serverid);

我的猜测Apiplatform是否能够采用'/api/server/1',并且明白这是IRI,它是实体,并且控制器不是,但是有任何方法可以解决吗?

谢谢

i have i guess simple problem, but i cant get it working properly:

Im getting this error: App\Entity\Commands::setServerID(): Argument #1 ($serverID) must be of type ?App\Entity\Servers, string given
And when i try to do add new element via api-platform its working correctly:

enter image description here

And here is result:

enter image description here

But when i try to do it via postman or in my controller in symfony im getting error mentioned in begining:

        $commandDB = new Commands();
        $commandDB->setCommand($command);
        $commandDB->setStatusCode(1);
        $commandDB->setOutput("added to DB");
        $commandDB->setCreatedAt($date);
        $commandDB->setServerID($serverid);

My guess is that apiplatform was able to take '/api/servers/1' and understand that this is IRI and it is entity, and controller is not, but is there's any way to fix it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2025-02-20 16:40:46

错误说您必须通过一个实体,而不是ID。执行这样的操作:

将ManagerRegistry实例注入您的控制器

private ManagerRegistry $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
{
    $this->managerRegistry = $managerRegistry;
}

,然后获取服务器对象并将其设置为命令实体

$server = $this->managerRegistry->getRepository(Servers::class)->find($serverid);
$commandDB->setServerID($server);

The error says that you must pass in an entity, not an id. Do something like this:

Inject a ManagerRegistry instance into your controller

private ManagerRegistry $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
{
    $this->managerRegistry = $managerRegistry;
}

Then get your Servers object and set it into the Commands entity

$server = $this->managerRegistry->getRepository(Servers::class)->find($serverid);
$commandDB->setServerID($server);
千と千尋 2025-02-20 16:40:46

那么,我在@harvey Dent的帮助下解决了
我的主要问题是让对象在setServerid()中传递;
Harvey建议ManagerRegistry,但我使用EntityManagerInterface而不是因为它无法正常工作(无响应)。

这是对我有用的代码的一部分:

$server = $em->getRepository(Servers::class)->findOneBy(['id' => $serverid,])

再次感谢Harvey的想法!

Well then, i solved it with help of @Harvey Dent
my main problem was to get object to pass it in setServerID();
Harvey suggested ManagerRegistry but i used EntityManagerInterface instead becouse it was not working properly for me(null response).

this is part of code that is working for me:

$server = $em->getRepository(Servers::class)->findOneBy(['id' => $serverid,])

Thanks again Harvey for idea!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文