Symfony - 反序列化包含ManyToOne属性的实体

时间:2018-03-05 10:17:42

标签: php symfony doctrine

我有一个实体" Eleve" :

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
* @ORM\Entity
* @ORM\Table()
*/
class Eleve
{

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * 
 * @Serializer\Groups({"get"})
 */
private $id;

/**
 * @ORM\Column(type="string", length=100)
 * 
 * @Serializer\Groups({"get"})
 */
private $nom;

/**
 * @ORM\Column(type="string", length=100)
 * 
 * @Serializer\Groups({"get"})
 */
private $prenom;

/**
 * @ORM\ManyToOne(targetEntity="Promotion", cascade={"all"}, fetch="EAGER")
 * @ORM\JoinColumn(name="promotion_id", referencedColumnName="id")
 * 
 * @Serializer\Groups({"promos"})
 */
private $promotion;

/**
 * @ORM\OneToMany(targetEntity="NoteModule", mappedBy="module", cascade={"persist"})
 * 
 * @Serializer\Groups({"notes"})
 */
private $notes;


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

public function getNom()
{
    return $this->nom;
}

public function setNom($nom)
{
    $this->nom = $nom;

    return $this;
}

public function getPrenom()
{
    return $this->prenom;
}

public function setPrenom($prenom)
{
    $this->prenom = $prenom;

    return $this;
}

public function getPromotion()
{
    return $this->promotion;
}

public function setPromotion($promotion)
{
    $this->promotion = $promotion;

    return $this;
}

public function getNotes()
{
    return $this->notes;
}

public function setNotes($notes)
{
    $this->notes = $notes;

    return $this;
}
}

和#34; Eleve&#34;实体加入&#34;促销&#34;:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;

/**
 * @ORM\Entity
 * @ORM\Table()
 */
class Promotion
{

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * 
 * @Serializer\Groups({"get"})
 */
private $id;

/**
 * @ORM\Column(type="string", length=100)
 * 
 * @Serializer\Groups({"get"})
 */
private $title;

/** 
 * @ORM\Column(type="integer") 
 * 
 * @Serializer\Groups({"get"})
 */
private $annee;

/**
 * @ORM\ManyToOne(targetEntity="Formation", cascade={"all"}, fetch="EAGER")
 * 
 * @Serializer\Groups({"formations"})
 */
private $formation;

/**
 * @ORM\OneToMany(targetEntity="Eleve", mappedBy="promotion", cascade={"persist"})
 * 
 * @Serializer\Groups({"eleves"})
 */
private $eleves;


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

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

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

    return $this;
}

public function getAnnee()
{
    return $this->annee;
}

public function setAnnee($annee)
{
    $this->annee = $annee;

    return $this;
}

public function getFormation()
{
    return $this->formation;
}

public function setFormation($formation)
{
    $this->formation = $formation;

    return $this;
}

public function getEleves()
{
    return $this->eleves;
}

public function setEleves($eleves)
{
    $this->eleves = $eleves;

    return $this;
}
}

当我想通过Http Post发送这个Json文件时:

[
    {
        "nom":"EleveName",
        "prenom":"EleveFirstName",
        "promotion":1
    }
]

我有以下错误:

  

&#34; message&#34;:&#34;无效数据\&#34; 1 \&#34;(整数),预期\&#34; AppBundle \ Entity \ Promotion \&#34;。 &#34;,

我认为这意味着他想要一个&#34;促销&#34;实体而不是整数。 是否有可能只像我在json中那样提供促销ID,或者我被迫通过整个实体?

我使用FosRestBundle来执行我的http请求,我使用Doctrine作为数据库。 我真的不知道如何处理这个问题,我在网上找不到任何相关信息。也许你知道我可以在哪里寻求帮助。

有人可以帮助我吗?

谢谢!

0 个答案:

没有答案