持久对象到数据库错误

时间:2018-05-29 15:04:38

标签: symfony orm doctrine persistent symfony4

我有这个错误:

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

我的AddType

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下拉列表中)保存到数据库?

由于

1 个答案:

答案 0 :(得分:1)

您可能在EntityType表单中有一个AddType字段来处理guru_id属性,它会自动将提交的值(User的实例)映射为{{1 }} 方法。这就是为什么:

setGuruId(?int $guru_id)

您有一些选项可以解决这个问题,但最好的方法是将Expected argument of type "integer or null", "App\Entity\User" given 映射更改为guru_idManyToOne的关系:

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)); 组件已经完成了此过程。

作为一种快速解决方法,您可以修复此问题,删除typehint:

Form