我正在尝试在Symfony 4中制作一个简单的留言簿,我的目标是可以向该留言簿添加一条消息,但是在此之前, 内部激活。 我现在正在激活站点上,但是有一个我无法真正解决的问题。错误消息如下:
将实体绑定到仅可用于以下条件的实体查询参数 有一个标识符。
我想做的是,在激活器表单未激活消息的ID 后, >,它从提供的表单中获取数据(ID),然后 Doctrine查找该ID的特定行。然后,它将 smallint(非布尔型)“ isActive” 行更改为 1 ,以便留言簿知道要显示的消息。
在“ $ message = $ repository-> find($ id); ” symfony无法将我的表单数据与查询参数绑定。我已经尝试过this的答案,但是也没有用。
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()
));
}
}
Entity / 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;
}
}
答案 0 :(得分:0)
谢谢你。 我的问题是我需要一个数组而不是字符串。愚蠢的我。我改变了
public function setId(Array $id)
{
$this->id = $id;
}
到
public function setId(string $id)
{
$this->id = $id;
}