0

Gedmo Doctrine Extensions を使用して、カテゴリをネストされたセットとして処理しています。REST API を構築しており、カテゴリを作成するルートがあります。ルート カテゴリまたは子カテゴリを作成できるようにしたいと考えています。エンティティはこちら

<?php
namespace AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="bo_categories")
 * use repository for handy tree functions
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
 */
class Category
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string", length=64)
     */
    private $name;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(name="lft", type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(name="rgt", type="integer")
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\Column(name="root", type="integer", nullable=true)
     */
    private $root;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;

    public function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setParent(Category $parent = null)
    {
        $this->parent = $parent;
    }

    public function getParent()
    {
        return $this->parent;
    }
}

フォームはこちら

<?php

namespace AppBundle\Form\Type\Classification;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CategoryFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name')
                ->add('parent', 'entity', array(
                        'class'    => 'AppBundle:Category',
                        'property' => 'id',
                    ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'      => 'AppBundle\Entity\Category',
            'csrf_protection' => false,
        ));
    }

    public function getName()
    {
        return 'api_category';
    }
}

そしてコントローラーはこちら

/**
     * @Route("/create")
     * @Security("has_role('ROLE_SUPER_ADMIN')")
     * @Rest\View
     */
    public function postCreateAction(Request $request)
    {
        $categoryManager = $this->get('app.manager.category');

        $category = $categoryManager->createNew();

        $form = $this->createForm(new CategoryFormType(), $category);

        // $category->setParent(10);

        $form->handleRequest($request);

        if ($form->isValid()) {

            $categoryManager->save($category);

            return new Response('', 201);
        } else {
            return $this->view($form, 400);
        }

    }

子カテゴリを作成したい場合は、問題なく動作します。しかし、フォームの「親」フィールドを削除せずにルートカテゴリを作成したい場合、このエラーが発生します

An exception occurred while executing 'SELECT b0_.id AS id0, b0_.name AS name1, b0_.lft AS lft2, b0_.lvl AS lvl3, b0_.rgt AS rgt4, b0_.root AS root5, b0_.parent_id AS parent_id6 FROM bo_categories b0_ WHERE b0_.id IN (?)' with params [""]:\n\nSQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "", "class": "Doctrine\DBAL\DBALException

なぜこのエラーが発生するのですか? フォームで「親」の値を空/nullにすることはできませんか?

4

1 に答える 1