一对多插入错误的表

时间:2019-05-17 13:28:08

标签: api symfony

当我尝试创建带有类别api的新啤酒时,我有一个关系,其中一个表名为“ 类别”,另一个表名为“ Beer ”错误。

BeerController

/**
 * @Route("/", methods={"POST"})
 * @param  Request $request
 * @return Response
 */
public function saveAction(Request $request){

    $data_arr = json_decode($data = $request->getContent(),true);
    $beer = new Beer();
    $category = new Category();

    $beer->getCategory();
    $form = $this->createForm(BeerType::class, $beer);
    $form->submit($data_arr);

    $beer->setCategory($category);
    $doctrine = $this->getDoctrine()->getManager();
    $doctrine->persist($category);
    $doctrine->merge($beer);
    $doctrine->flush();
    return new JsonResponse([$data_arr], 200);
}

啤酒实体

/**
 * Beer
 *
 * @ORM\Table(name="beers")
 * @ORM\Entity
 */
class Beer
{

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="beer", cascade={"persist", "remove" }))
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;

    /**
     * @return mixed
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * @param mixed $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }

}

CategoryEntity

class Category
{
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Beer", mappedBy="category")
     */
    private $beer;

    public function __construct()
    {
        $this->beer = new ArrayCollection();
    }
}

啤酒类型(窗体)

class BeerType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array 
    $options)
    {
        $builder->add('name')
            ->add('description')
            ->add('price')
            ->add('slug')
            ->add('type')
            ->add('abv')
            ->add('brewedby')
            ->add('createdat')
            ->add('updatedat')
            ->add('category', EntityType::class, [
                    'class' => Category::class
                ]) 
   ;

console.log(错误)

  

”执行'INSERT INTO类别(名称)时发生异常   带有参数[null]的VALUES(?)'   SQLSTATE [23000]:违反完整性约束:1048列“名称”   不能为空”

正如问题本身所言,当我尝试创建具有类别(已创建)的新啤酒时,控制器尝试创建新类别,但是我想创建具有已创建类别的新啤酒。

编辑:

当我通过api从啤酒中发送数据时,类别正常发送,但是当我尝试用get推送啤酒时,类别就没有更多的啤酒了。

张贴啤酒

{abv: 7, brewedby: "Péttrin Miranda Souza", category_id: "7", description: "Descrição da cerva",…}
abv: 7
brewedby: "Péttrin Miranda Souza"
category_id: "7"
description: "Descrição da cerva"
name: "Beer"
price: "25"
slug: "mugger"
type: "Cerveja bavaria"

喝啤酒

5: {id: 19, name: "Beer", description: "Descrição da cerva", price: 25, slug: "mugger",…}
abv: 7
brewedby: "Péttrin Miranda Souza"
createdat: "2019-05-20T09:28:08-03:00"
description: "Descrição da cerva"
id: 19
name: "Beer"
price: 25
slug: "mugger"
type: "Cerveja bavaria"
updatedat: "2019-05-20T09:28:08-03:00"

1 个答案:

答案 0 :(得分:1)

由于您已经在表单中选择了类别实体,因此无需实例化类别实体或将其持久化(遇到的错误来自创建新实例并持久化-而不是使用现有类别在表单数据绑定中连接到啤酒实体的引用)。您可以尝试以下操作版本:

/**
 * @Route("/", methods={"POST"})
 * @param  Request $request
 * @return Response
 */
public function saveAction(Request $request){

    $doctrine = $this->getDoctrine()->getManager();

    $data_arr = json_decode($data = $request->getContent(),true);
    $beer = new Beer();

    $form = $this->createForm(BeerType::class, $beer);
    $form->submit($data_arr);

    $category = $doctrine->getRepository(Category::class)->find($data_arr['category_id']);
    $beer->setCategory($category);

    $doctrine->persist($beer);
    $doctrine->flush();

    return new JsonResponse([$data_arr], 200);
}

在附加评论后更新了答案。因此,现在根据给定的参数ID明确获取类别。另一个解决方案-以前的简单控制器代码没有对类别进行任何特殊提取-将使用表单数据转换器(symfony data transormer),该数据转换器将int id自动映射到类别对象。