将对象持久化到数据库错误
Persisting object to database error
我有这个错误:
Expected argument of type "integer or null", "App\Entity\User" given
我不明白,我理解这个错误,但我不确定它为什么会出现。
这是我的 AddController.php 文件:
<?php
namespace App\Controller\Tag;
use App\Entity\Tag;
use App\Form\Tag\AddType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Request;
class AddController extends Controller
{
public function add(Request $request)
{
$tag = new Tag();
$form = $this->createForm(AddType::class, $tag);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$pageInfo = $form->getData();
$description = $pageInfo['description'];
$name = $pageInfo['name'];
$guru = $pageInfo['guru_id'];
$createdTs = new \DateTime();
$tag->setApproved(false);
$tag->setDescription($description);
$tag->setName($name);
$tag->setGuruId((is_int($guru) ? $guru : null));
$tag->setCreatedTs($createdTs);
try {
$entityManager->persist($tag);
$entityManager->flush();
$this->addFlash('success', 'Tag Submitted for review! '. $guru);
} catch (Exception $e) {
$this->addFlash('danger', 'Something went skew-if. Please try again.');
}
return $this->redirectToRoute('tag_add');
}
return $this->render('tag/add.html.twig', array('form' => $form->createView()));
}
}
初始加载有效,但提交表单会产生上述错误。但我不确定为什么。即使我这样重新排序我的行:
$tag = new Tag();
$tag->setGuruId(null);
我的标签实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
*/
class Tag
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=125)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $description;
/**
* @ORM\Column(type="boolean")
*/
private $approved;
/**
* @ORM\Column(type="datetime")
*/
private $created_ts;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_edit_ts;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $guru_id;
public function getId()
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getApproved(): ?bool
{
return $this->approved;
}
public function setApproved(bool $approved): self
{
$this->approved = $approved;
return $this;
}
public function getCreatedTs(): ?\DateTimeInterface
{
return $this->created_ts;
}
public function setCreatedTs(\DateTimeInterface $created_ts): self
{
$this->created_ts = $created_ts;
return $this;
}
public function getLastEditTs(): ?\DateTimeInterface
{
return $this->last_edit_ts;
}
public function setLastEditTs(\DateTimeInterface $last_edit_ts): self
{
$this->last_edit_ts = $last_edit_ts;
return $this;
}
public function getGuruId(): ?int
{
return $this->guru_id;
}
public function setGuruId(?int $guru_id): self
{
$this->guru_id = $guru_id;
return $this;
}
}
我的添加类型
use App\Entity\Tag;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AddType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class)
->add('description', TextareaType::class)
->add('guru_id', EntityType::class, array(
'class' => User::class,
'choice_label' => 'username'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Tag::class
));
}
}
它仍然给出相同的错误,所以我真的不确定它是如何生成的...我如何将整数(从 EntityType 下拉列表中获取)保存到数据库?
谢谢
可能您在 AddType
表单中有一个 EntityType
字段来处理 guru_id
属性 并且它正在映射提交的值(User
的实例) 自动使用 setGuruId(?int $guru_id)
方法。这就是为什么:
Expected argument of type "integer or null", "App\Entity\User" given
您有一些解决方法,但最好的方法可能是将 guru_id
映射更改为 ManyToOne
与 User
的关系:
// Tag entity
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $guru;
public function getGuru(): ?User
{
return $this->guru;
}
public function setGuru(?User $guru): self
{
$this->guru = $guru;
return $this;
}
另外,我会删除这段代码:
$tag->setDescription($description);
$tag->setName($name);
$tag->setGuruId((is_int($guru) ? $guru : null));
因为此过程已由 Form
组件在提交数据后完成。
作为快速解决方法,您可以通过删除类型提示来解决此问题:
public function setGuruId($guru_id): self
{
$this->guru_id = $guru_id instanceof User ? $guru_id->getId() : $guru_id;
return $this;
}
我有这个错误:
Expected argument of type "integer or null", "App\Entity\User" given
我不明白,我理解这个错误,但我不确定它为什么会出现。
这是我的 AddController.php 文件:
<?php
namespace App\Controller\Tag;
use App\Entity\Tag;
use App\Form\Tag\AddType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Request;
class AddController extends Controller
{
public function add(Request $request)
{
$tag = new Tag();
$form = $this->createForm(AddType::class, $tag);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$pageInfo = $form->getData();
$description = $pageInfo['description'];
$name = $pageInfo['name'];
$guru = $pageInfo['guru_id'];
$createdTs = new \DateTime();
$tag->setApproved(false);
$tag->setDescription($description);
$tag->setName($name);
$tag->setGuruId((is_int($guru) ? $guru : null));
$tag->setCreatedTs($createdTs);
try {
$entityManager->persist($tag);
$entityManager->flush();
$this->addFlash('success', 'Tag Submitted for review! '. $guru);
} catch (Exception $e) {
$this->addFlash('danger', 'Something went skew-if. Please try again.');
}
return $this->redirectToRoute('tag_add');
}
return $this->render('tag/add.html.twig', array('form' => $form->createView()));
}
}
初始加载有效,但提交表单会产生上述错误。但我不确定为什么。即使我这样重新排序我的行:
$tag = new Tag();
$tag->setGuruId(null);
我的标签实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
*/
class Tag
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=125)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $description;
/**
* @ORM\Column(type="boolean")
*/
private $approved;
/**
* @ORM\Column(type="datetime")
*/
private $created_ts;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_edit_ts;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $guru_id;
public function getId()
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getApproved(): ?bool
{
return $this->approved;
}
public function setApproved(bool $approved): self
{
$this->approved = $approved;
return $this;
}
public function getCreatedTs(): ?\DateTimeInterface
{
return $this->created_ts;
}
public function setCreatedTs(\DateTimeInterface $created_ts): self
{
$this->created_ts = $created_ts;
return $this;
}
public function getLastEditTs(): ?\DateTimeInterface
{
return $this->last_edit_ts;
}
public function setLastEditTs(\DateTimeInterface $last_edit_ts): self
{
$this->last_edit_ts = $last_edit_ts;
return $this;
}
public function getGuruId(): ?int
{
return $this->guru_id;
}
public function setGuruId(?int $guru_id): self
{
$this->guru_id = $guru_id;
return $this;
}
}
我的添加类型
use App\Entity\Tag;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AddType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class)
->add('description', TextareaType::class)
->add('guru_id', EntityType::class, array(
'class' => User::class,
'choice_label' => 'username'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Tag::class
));
}
}
它仍然给出相同的错误,所以我真的不确定它是如何生成的...我如何将整数(从 EntityType 下拉列表中获取)保存到数据库?
谢谢
可能您在 AddType
表单中有一个 EntityType
字段来处理 guru_id
属性 并且它正在映射提交的值(User
的实例) 自动使用 setGuruId(?int $guru_id)
方法。这就是为什么:
Expected argument of type "integer or null", "App\Entity\User" given
您有一些解决方法,但最好的方法可能是将 guru_id
映射更改为 ManyToOne
与 User
的关系:
// Tag entity
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $guru;
public function getGuru(): ?User
{
return $this->guru;
}
public function setGuru(?User $guru): self
{
$this->guru = $guru;
return $this;
}
另外,我会删除这段代码:
$tag->setDescription($description);
$tag->setName($name);
$tag->setGuruId((is_int($guru) ? $guru : null));
因为此过程已由 Form
组件在提交数据后完成。
作为快速解决方法,您可以通过删除类型提示来解决此问题:
public function setGuruId($guru_id): self
{
$this->guru_id = $guru_id instanceof User ? $guru_id->getId() : $guru_id;
return $this;
}