首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony5:使用REST保存相关(oneToMany)实体

Symfony5:使用REST保存相关(oneToMany)实体
EN

Stack Overflow用户
提问于 2021-08-28 21:20:06
回答 1查看 140关注 0票数 0

我有两个实体类:产品和价格。产品价格很高。这些关系被定义为(只显示了相关代码):

Product.php

代码语言:javascript
复制
public function __construct()
{
    $this->prices = new ArrayCollection();
}

/**
 * @var Collection|Price[]
 * @ORM\OneToMany(targetEntity="Price", mappedBy="product", cascade={"persist", "remove"})
*/
private Collection $prices;

/**
 * @return Collection|Price[]
 */
public function getPrices()
{
    return $this->prices;
}

/**
 * @param Collection|Price[] $prices
 */
public function setPrices(Collection $prices) : void
{
    $this->prices = $prices;
}

Price.php

代码语言:javascript
复制
/**
 * @ORM\ManyToOne(targetEntity="Product", inversedBy="prices")
*/
private Product $product;

public function getProduct() : Product
{
    return $this->product;
}

public function setProduct(?Product $product) : self
{
    $this->product = $product;
    return $this;
}

ProductType::buildForm()

代码语言:javascript
复制
$builder
->add('prices', EntityType::class, [
    'class' => Price::class,
    'multiple' => true,
    'constraints' => [
        new NotNull(),
    ],
]);

ProductController::add()

代码语言:javascript
复制
public function add(Request $request, EntityManagerInterface $manager) : Response
{
    $form = $this->container->get('form.factory')->createNamed('', ProductType::class);
    $form->handleRequest($request);

    if (! $form->isSubmitted() || !$form->isValid()) {
        return $this->handleView($this->view($form, Response::HTTP_BAD_REQUEST));
    }

    /** @var Product $product */
    $product = $form->getData();

    $manager->persist($product);
    $manager->flush();

    return $this->respond($product, Response::HTTP_CREATED);
}

请求JSON

代码语言:javascript
复制
{
  "name": "added",
  "prices": [
        {
        "currency": "EUR",
        "value": 100
        },
        {
        "currency": "PLN",
        "value": 400
        }
    ],
  "description": "test desc"
}

JSON响应

代码语言:javascript
复制
{
  "code": 400,
  "message": "Validation Failed",
  "errors": {
    "children": {
      "name": {},
      "description": {},
      "prices": {
        "errors": [
          "Not valid"
        ]
      }
    }
  }
}

具体而言,问题在于价格--通过空价格数组可以毫无错误地创建产品。我用FOSRestBundle。

我在谷歌上搜索了很多小时,但没有成功。我刚接触过Symfony,所以我很可能错过了一些显而易见的东西:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-31 19:37:55

问题出现在ProductType类中。

而不是:

代码语言:javascript
复制
$builder
->add('prices', EntityType::class, [
    'class' => Price::class,
    'multiple' => true,
    'constraints' => [
        new NotNull(),
    ],
]);

应该用

代码语言:javascript
复制
$builder
->add('prices', CollectionType::class, [
    'entry_type' => PriceType::class,
    'allow_add' => true,
    'constraints' => [
        new NotNull(),
    ],
]);

答案找到这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68968200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档