无法将表单数据绑定到学说查找功能。 "ORMInvalidArgumentException"



我正试图在Symfony 4中制作一个简单的留言簿,我的目标是您可以向留言簿中添加一条消息,但在此之前,它需要内部激活我现在正在激活网站,但有一个问题我真的无法解决。错误消息如下:

将实体绑定到查询参数只允许具有标识符。

我想做的是,在您在ActivatorForm中键入未激活消息的ID后,它会从给定的表单获取数据(ID),并且Doctrine会找到该ID的特定。然后,它将smallint(非布尔值)"isActive">行更改为1,以便留言簿知道要显示哪些消息

">$message=$repository->find($id);">处,symfony无法将我的表单数据与查询参数绑定。我已经试过这个答案了,但也没用。

GBActivatorController.php:

use AppEntityGuestbook;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeNumberType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;

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()
));
}
}

Entity/Guestbook.php:

namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryGuestbookRepository")
*/
class Guestbook
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
public $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $email;
/**
* @ORMColumn(type="string", length=255)
*/
private $text;
/**
* @ORMColumn(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;
}
}

谢谢Eakethet。我的问题是我需要一个数组而不是字符串。愚蠢的我,我换了

public function setId(Array $id)
{
$this->id = $id;
}

public function setId(string $id)
{
$this->id = $id;
}

相关内容

  • 没有找到相关文章

最新更新