php - 无法将表单数据绑定到学说查找功能。“ORMInvalidArgumentException”
问题描述
我正在尝试在 Symfony 4 中制作一个简单的留言簿,我的目标是您可以向留言簿添加一条消息,但在此之前,它需要在内部激活。 我现在正在处理激活站点,但有一个问题我无法真正解决。错误信息如下:
仅允许具有标识符的实体将实体绑定到查询参数。
我要做的是,在您在Activator Form中输入未激活消息的 ID后,它会从给定的表单中获取数据(ID) , 并且Doctrine 会找到该 ID 的特定行. 然后,它将smallint 行(不是布尔值)“isActive”更改为1,因此留言簿知道要显示哪些消息。
在“ $message = $repository->find($id); ”处, symfony 无法将我的表单数据与查询参数绑定。我已经尝试过这个答案,但也没有用。
GBActivatorController.php:
use App\Entity\Guestbook;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class GBActivatorController extends AbstractController
{
public function index(Request $request)
{
//sets up the entity guestbook and the entitymanager from doctrine
$guestbook = new Guestbook();
$entityManager = $this->getDoctrine()->getManager();
//get all messages
$list = $this->getDoctrine()
->getRepository(Guestbook::class)
->findAll();
//create form for activating a non-activated message
$activator = $this->createFormBuilder($guestbook)
->add('id', NumberType::class,
array(
'mapped' => false,
))
->add('activate', SubmitType::class, ['label' => 'Activate'])
->getForm();
$activator->handleRequest($request);
/*when the submit button is clicked, get the data and
find the row with the specific id that was given.*/
if($activator->isSubmitted())
{
//gets the data from the form
$id = $activator->getData();
//sets up repository, so i dont need to type it again
$repository = $this->getDoctrine()->getRepository(Guestbook::class);
//(should) find the row
$message = $repository->find($id);
//activates row. (isActive = 1 instead of isActive = 0)
$message->setIsActive(1);
//persist and then execute to table
$entityManager->persist($message);
$entityManager->flush();
}
//renders the template
return $this->render('GBActivator.html.twig', array(
'list' => $list,
'activator' => $activator->createView()
));
}
}
实体/Guestbook.php:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\GuestbookRepository")
*/
class Guestbook
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
public $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
*/
private $text;
/**
* @ORM\Column(type="smallint")
*/
private $isActive;
public function setId(Array $id)
{
$this->id = $id;
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
public function getIsActive(): ?int
{
return $this->isActive;
}
public function setIsActive(int $isActive): self
{
$this->isActive = $isActive;
return $this;
}
}
解决方案
谢谢埃克塞特。我的问题是我需要一个数组而不是字符串。愚蠢的我。我变了
public function setId(Array $id)
{
$this->id = $id;
}
至
public function setId(string $id)
{
$this->id = $id;
}
推荐阅读
- forms - 我怎样才能得到正确的日期
- r - 将许多逻辑列转换为字符串或因子
- python - python/tkinter,如何将东西“固定”在它们的位置?
- oauth - Curity 密码流返回“invalid_grant”,但没有详细信息
- php - 请告诉我。我在登录名/密码字段上方有一个关于密码不正确的错误输出。如何在主页上显示它?
- typescript - 如何根据接口的键创建模板文字类型?
- kdb - 在 KDB 中获取表的长度/尺寸的最短方法是什么?
- sql - 在过程中创建序列时出现权限问题
- azure - 在 ISE 环境中扩展逻辑应用程序和天蓝色函数
- reactjs - 如何使用react js显示多个文件名