如何在Symfony 4中修复子对象的转换?

时间:2019-04-01 07:37:43

标签: php symfony4

我有一条实体文章。 在文章内部,我有一个实体作者。 使用ParamConverter和转换器fos_rest.request_body,我尝试将数据转换为实体或对象,例如Datetime。 主要实体还可以。子实体未转换。 如何修复子实体/对象转换?

主要实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table()
 */
class Article
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * @ORM\ManyToOne(targetEntity="Author", cascade={"all"}, fetch="EAGER")
     */
    private $author;

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

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    public function getAuthor()
    {
        return $this->author;
    }

    public function setAuthor(Author $author)
    {
        $this->author = $author;
    }
}

子实体:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table()
 */
class Author
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $fullname;

    /**
     * @ORM\Column(type="text")
     */
    private $biography;

    /**
     * @ORM\OneToMany(targetEntity="Article", mappedBy="author", cascade={"persist"})
     */
    private $articles;

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

    public function getFullname()
    {
        return $this->fullname;
    }

    public function setFullname($fullname)
    {
        $this->fullname = $fullname;
    }

    public function getBiography()
    {
        return $this->biography;
    }

    public function setBiography($biography)
    {
        $this->biography = $biography;
    }

    public function getArticles()
    {
        return $this->articles;
    }
}

控制器:

<?php

namespace App\Controller;

use App\Entity\Article;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class ArticleController extends Controller
{
    /**
     * @Rest\Post(
     *    path = "/articles",
     *    name = "app_article_create"
     * )
     * @Rest\View(StatusCode = 201)
     * @ParamConverter("article", converter="fos_rest.request_body")
     */
    public function createAction(Article $article)
    {
                dump($article); die;

        return $article;
    }
}

fos_rest.yaml:

sensio_framework_extra:
    request: { converters: true }

fos_rest:
    body_converter:
        enabled: true
        validate: true
        validation_errors_argument: violations
    view:
        formats: { json: true, xml: false, rss: false }
        view_response_listener: true
    serializer:
        serialize_null: true
    format_listener:
        rules:
            - { path: '^/', priorities: ['json'], fallback_format: 'json' }

framework.yaml:

    serializer:
        enabled: true

将数据发布到URL时,出现错误消息:

"error": {
        "code": 400,
        "message": "Bad Request",
        "exception": [
            {
                "message": "Expected argument of type \"App\\Entity\\Author\", \"array\" given at property path \"author\".",
                "class": "Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException",

我想获得有效的实体文章

0 个答案:

没有答案