php - Symfony - '' 类型的预期值改为“字符串”
问题描述
我正在尝试将 id 从一个形成表传递到票证表,但我无法传递此错误..
关联字段“App\Entity\Ticket#$formation”的“App\Entity\Formation”类型的预期值,改为“字符串”。
形成实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\FormationRepository")
*/
class Formation
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $formations = [];
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $customerName;
...
票务实体:
/**
* @ORM\MappedSuperclass()
*/
class Ticket implements TicketInterface
{
use Timestampable;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Symfony\Component\Security\Core\User\UserInterface")
* @ORM\JoinColumn(name="author", referencedColumnName="id", nullable=true)
*/
private $author;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Formation")
* @ORM\JoinColumn(nullable=false)
*/
private $formation;
...
我的票控制器addTicket()
:
public function addTicket(Request $request, TicketManager $ticketManager): Response
{
$ticket = $ticketManager->newClass();
$user = $this->getUser();
$formationId = $user->getFormationId()->getId();
$ticketForm = $this->createForm(TicketForm::class, $ticket);
$ticketForm->handleRequest($request);
if ($ticketForm->isSubmitted() && $ticketForm->isValid()) {
$ticketManager->createTicket($user, $ticket, $formationId);
$this->addFlash('success', 'Le ticket est en ligne !');
return $this->redirectToRoute('ticketing_list');
}
return $this->render($this->ticketingTemplates['new'], [
'form' => $ticketForm->createView(),
]);
}
我的票务管理器createTicket()
:
/**
* @param UserInterface $user
* @param TicketInterface $ticket
* @param string $formationId
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function createTicket(UserInterface $user, TicketInterface $ticket, string $formationId)
{
$status = $this->ticketStatusManager->getOpenStatus();
$ticket->setStatus($status)->setFormationId($formationId)->setAuthor($user)->setPublic(false);
if (!$this->isTicketRestrictionEnabled()) {
$ticket->setPublicAt(new \DateTime())->setPublic(true);
}
$this->persistAndFlush($ticket);
}
我希望在添加票证时,将“formationId”保存在票证表中。其余的都很好,只有formationId的注册不起作用
编辑 :
票证实体setFormationId()
的定义:
public function setFormation($formation): self
{
$this->formation = $formation;
return $this;
}
我的票控制器addTicket()
:
public function addTicket(Request $request, TicketManager $ticketManager): Response
{
$ticket = $ticketManager->newClass();
$user = $this->getUser();
$formationId = $user->getFormationId();
$ticketForm = $this->createForm(TicketForm::class, $ticket);
$ticketForm->handleRequest($request);
if ($ticketForm->isSubmitted() && $ticketForm->isValid()) {
$ticketManager->createTicket($user, $ticket, $formationId);
$this->addFlash('success', 'Le ticket est en ligne !');
return $this->redirectToRoute('ticketing_list');
}
return $this->render($this->ticketingTemplates['new'], [
'form' => $ticketForm->createView(),
]);
}
同样的错误
解决方案
/**
* @param UserInterface $user
* @param TicketInterface $ticket
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function createTicket(UserInterface $user, TicketInterface $ticket, Formation $formation)
{
$status = $this->ticketStatusManager->getOpenStatus();
$ticket->setStatus($status)->setFormation($formation)->setAuthor($user)->setPublic(false);
if (!$this->isTicketRestrictionEnabled()) {
$ticket->setPublicAt(new \DateTime())->setPublic(true);
}
$this->persistAndFlush($ticket);
}
推荐阅读
- python - Pandas Duplicate() 一次返回所有重复项,除了一行
- memcached - Vtiger CRM 开源版本 7x(Meme Cache / Varnish?)
- kotlin - 预期为 BEGIN_OBJECT,但在第 1 行第 2 列路径为 BEGIN_ARRAY $ ERROR
- telegram - 广播扩展将 PixelBuffer 发送到 OpenTok 服务器
- python - 在 ffmpeg 函数参数中传递字节而不是文件路径
- android - 创建一个没有 Json 数组名称的 Json 数组
- javascript - 更改 SVG 高度而不更改宽度或使其倾斜/拉伸
- postgresql - ORDER BY 和 ROW_NUMBER() 优先级 - DB2 和 PostgreSQL 返回不同的结果,相同的查询
- reactjs - 无法播放接收到的音频数据/缓冲区 Reactjs
- rust - battl 模拟引擎中的 Rust 借用检查器