2013-09-23 79 views
2

我遇到FOSRestBundle問題。當我跑過來集合中取得動作我得到這樣的(JSON格式)事端:FOSRestBundle獲取屬性與發佈屬性不同

{"entities":[{"id":1,"main_diagnostic_name":"0000","main_diagnostic_code":"1111","start_date":null,"end_date":null,"patient":{"id":1,"identifier":"3874908","first_name":"Pedro","surname":"Barroso","gender":"M","birth_date":"1972-01-01T00:00:00+0000","photo":null,"communication":"ab","active":true,"nhc":null,"allergy":false,"episodes":[]},"prescriptions":[],"id_bed":1,"separate":null,"special_diet":false,"contagious_diseases":false,"aggressive":false}]} 

但是,當我做後調用了同一個集合我NEET把變量名:

{"mainDiagnosticName":"XXXX","mainDiagnosticCode":"1111","startDate":null,"endDate":null,"patient":"1","idBed":1,"separate":null,"specialDiet":false,"contagiousDiseases":false,"aggressive":false} 

我該怎麼做才能讓這兩個動作具有相同的行爲?爲什麼讓我返回下劃線的參數?

這是我的實體:

<?php 

namespace wathever; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Episode 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Strim\NursingBundle\Entity\EpisodeRepository") 
*/ 
class Episode { 

/** 
* @var integer 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
* @ORM\OneToMany(targetEntity="Prescription" , mappedBy="id") 
*/ 
private $id; 

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

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

/** 
* @var \DateTime 
* 
* @ORM\Column(name="startDate", type="datetime", nullable=true) 
*/ 
private $startDate; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="endDate", type="datetime", nullable=true) 
*/ 
private $endDate; 

/** 
* @ORM\ManyToOne(targetEntity="Patient", inversedBy="patient") 
* @ORM\JoinColumn(name="patient_id", referencedColumnName="id") 
*/ 
private $patient; 

/** 
* @ORM\OneToMany(targetEntity="Prescription" , mappedBy="episodes") 
*/ 
private $prescriptions; 

/** 
* @var integer 
* @ORM\Column(name="idBed", type="integer", nullable=true) 
*/ 
private $idBed; 

/** 
* @var boolean 
* @ORM\ManyToOne(targetEntity="Beds") 
* @ORM\JoinColumn(name="bed_id", referencedColumnName="id") */ 
private $separate; 

/** 
* @var boolean 
* 
* @ORM\Column(name="specialDiet", type="boolean") 
*/ 
// TODO esto como boleano no debería ser 
private $specialDiet; 

/** 
* @var boolean 
* 
* @ORM\Column(name="contagiousDiseases", type="boolean") 
*/ 
// TODO esto como boleano no debería ser 
private $contagiousDiseases; 

/** 
* @var boolean 
* 
* @ORM\Column(name="aggressive", type="boolean") 
*/ 
private $aggressive; 

/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() { 
    return $this->id; 
} 

/** 
* Set mainDiagnosticName 
* 
* @param string $mainDiagnosticName 
* @return Episode 
*/ 
public function setMainDiagnosticName($mainDiagnosticName) { 
    $this->mainDiagnosticName = $mainDiagnosticName; 

    return $this; 
} 

/** 
* Get mainDiagnosticName 
* 
* @return string 
*/ 
public function getMainDiagnosticName() { 
    return $this->mainDiagnosticName; 
} 

/** 
* Set mainDiagnosticCode 
* 
* @param string $mainDiagnosticCode 
* @return Episode 
*/ 
public function setMainDiagnosticCode($mainDiagnosticCode) { 
    $this->mainDiagnosticCode = $mainDiagnosticCode; 

    return $this; 
} 

/** 
* Get mainDiagnosticCode 
* 
* @return string 
*/ 
public function getMainDiagnosticCode() { 
    return $this->mainDiagnosticCode; 
} 

/** 
* Set startDate 
* 
* @param \DateTime $startDate 
* @return Episode 
*/ 
public function setStartDate($startDate) { 
    $this->startDate = $startDate; 

    return $this; 
} 

/** 
* Get startDate 
* 
* @return \DateTime 
*/ 
public function getStartDate() { 
    return $this->startDate; 
} 

/** 
* Set endDate 
* 
* @param \DateTime $endDate 
* @return Episode 
*/ 
public function setEndDate($endDate) { 
    $this->endDate = $endDate; 

    return $this; 
} 

/** 
* Get endDate 
* 
* @return \DateTime 
*/ 
public function getEndDate() { 
    return $this->endDate; 
} 

/** 
* Set patient 
* 
* @param integer $patient 
* @return Episode 
*/ 
public function setIdPatient($idPatient) { 
    $this->patient = $patient; 

    return $this; 
} 

/** 
* Get patient 
* 
* @return integer 
*/ 
public function getPatient() { 
    return $this->patient; 
} 

/** 
* Set idBed 
* 
* @param integer $idBed 
* @return Episode 
*/ 
public function setIdBed($idBed) { 
    $this->idBed = $idBed; 

    return $this; 
} 

/** 
* Get idBed 
* 
* @return integer 
*/ 
public function getIdBed() { 
    return $this->idBed; 
} 

/** 
* Set separate 
* 
* @param boolean $separate 
* @return Episode 
*/ 
public function setSeparate($separate) { 
    $this->separate = $separate; 

    return $this; 
} 

/** 
* Get separate 
* 
* @return boolean 
*/ 
public function getSeparate() { 
    return $this->separate; 
} 

/** 
* Set specialDiet 
* 
* @param boolean $specialDiet 
* @return Episode 
*/ 
public function setSpecialDiet($specialDiet) { 
    $this->specialDiet = $specialDiet; 

    return $this; 
} 

/** 
* Get specialDiet 
* 
* @return boolean 
*/ 
public function getSpecialDiet() { 
    return $this->specialDiet; 
} 

/** 
* Set contagiousDiseases 
* 
* @param boolean $contagiousDiseases 
* @return Episode 
*/ 
public function setContagiousDiseases($contagiousDiseases) { 
    $this->contagiousDiseases = $contagiousDiseases; 

    return $this; 
} 

/** 
* Get contagiousDiseases 
* 
* @return boolean 
*/ 
public function getContagiousDiseases() { 
    return $this->contagiousDiseases; 
} 

/** 
* Set aggressive 
* 
* @param boolean $aggressive 
* @return Episode 
*/ 
public function setAggressive($aggressive) { 
    $this->aggressive = $aggressive; 

    return $this; 
} 

/** 
* Get aggressive 
* 
* @return boolean 
*/ 
public function getAggressive() { 
    return $this->aggressive; 
} 

/** 
* Set patient 
* 
* @param \Strim\NursingBundle\Entity\Patient $patient 
* @return Episode 
*/ 
public function setPatient(\Strim\NursingBundle\Entity\Patient $patient = null) { 
    $this->patient = $patient; 

    return $this; 
} 

/** 
* Constructor 
*/ 
public function __construct($object = null) { 
    $this->prescriptions = new \Doctrine\Common\Collections\ArrayCollection(); 
    $this->patient = new \Doctrine\Common\Collections\ArrayCollection(); 
    if ($object){ 
     $this->setParameters($object); 
    } 
} 

public function setParameters($object){ 

    $vars = get_object_vars($object) 
      ; 
      foreach ($vars as $v){ 

call_user_func('set'.$v, $object->get($v)); 
      } 

    return $this; 
} 


/** 
* Add prescriptions 
* 
* @param \Strim\NursingBundle\Entity\Prescription $prescriptions 
* @return Episode 
*/ 
public function addPrescription(\Strim\NursingBundle\Entity\Prescription $prescriptions) { 
    $this->prescriptions[] = $prescriptions; 

    return $this; 
} 

/** 
* Remove prescriptions 
* 
* @param \Strim\NursingBundle\Entity\Prescription $prescriptions 
*/ 
public function removePrescription(\Strim\NursingBundle\Entity\Prescription $prescriptions) { 
    $this->prescriptions->removeElement($prescriptions); 
} 

/** 
* Get prescriptions 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getPrescriptions() { 
    return $this->prescriptions; 
} 

} 

這是我cgetAction:

/** 
* Collection get action 
* @var Request $request 
* @return array 
* @Rest\View() 
*/ 
public function cgetAction(Request $request) { 
    $em = $this->getDoctrine()->getManager(); 
    $entities = $em->getRepository('MyBundle:Episode')->findAll(); 
    return array(
     'entities' => $entities, 
    ); 
} 

我的POST操作:

/** 
* Collection post action -> Crear un elemento de la colección 
* @var Request $request 
* @return View|array 
* @Rest\View() 
*/ 
public function cpostAction(Request $request) { 
     $entity = new Episode(); 
     $form = $this->createForm(new EpisodeType(), $entity); 

     $form->bind($request); 
if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return new Response($request, 201); 
    } 

    return array(
     'form' => $form, 
    ); 
} 
+0

沒有人知道我已經看到了,在JMSSerializerBundle,但我正在尋找一個更好的解決辦法?:@SerializedName¶ 此批註可在一個屬性被定義爲屬性定義的序列化的名字。如果未定義,則該屬性將從駱駝案件翻譯成較小的下劃線名稱,例如, camelCase - > camel_case。 –

+0

看到這個鏈接:https://github.com/schmittjoh/serializer/issues/33然而,不清楚如何通過命名策略實現這個傳遞作爲FOSRest的一部分..我現在正在調查這一點,因爲是的,這是一個痛苦 – edmondscommerce

回答

2

具有相同的JSON格式爲「後「行動你必須使用FOSRestBundle View

這裏您cgetAction應該什麼樣子的摘錄:

記得把你的「使用」的語句use FOS\RestBundle\View\View;能夠爲用戶FosRestBungle視圖處理程序。

7

編輯:只有提交這個帖子之後,我已經注意到了,那這將是我的用例只使用IdenticalPropertyNamingStrategy與JMSSerializer交付容易得多,而不是壓倒一切和去配置方法進行說明。剩下的,因此,僅僅是

# file: app/config.yml 
parameters: 
    jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy 

它可以幫助您覆蓋JMSSerializer的默認駝峯命名策略。我已經擴展了CamelCaseNamingStrategy如下

// File: MyName/MyBundle/Serializer/CamelCaseNamingStrategy.php 
namespace MyName\MyBundle\Serializer; 
use JMS\Serializer\Metadata\PropertyMetadata; 

class CamelCaseNamingStrategy extends \JMS\Serializer\Naming\CamelCaseNamingStrategy 
{ 
    public function translateName(PropertyMetadata $property) 
    { 
     return lcfirst(parent::translateName($property)); 
    } 
} 

然後,您可以配置JMSSerializerBundle使用這個類來代替原來的

# file: app/config.yml 
parameters: 
    jms_serializer.camel_case_naming_strategy.class: MyName\MyBundle\Serializer\CamelCaseNamingStrategy 

最後,你需要禁用較低的外殼和分隔符,插入JMSSerializer通常會這樣做。

# still in app/config.yml 
jms_serializer: 
    property_naming: 
     separator: 
     lower_case: false 
+0

不錯!我真的不明白爲什麼屬性名稱是不完整的,因此不保留默認* – dmidz

+0

實際上這並不容易,因爲IdenticalPropertyNamingStrategy不考慮您的配置類型和內容 – dVaffection