2013-09-23 2 views
2

FOSRestBundle에 문제가 있습니다. 내가 컬렉션을 통해 GET 작업을 실행할 때 나는이 (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} 

두 동작 모두 동일한 동작을 수행 할 수있는 방법은 무엇입니까? 왜 내가 밑줄 친 매개 변수로 나를 돌려 주나요?

/** 
* 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 액션 : 이것은 내 cgetAction입니다

<?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; 
} 

} 

:

내 실체

/** 
* 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 모양을하는 방법에 대한 발췌 :

사용자 FosRestBungle보기 핸들러 수 있도록 당신의 "사용"문 use FOS\RestBundle\View\View;에 넣어해야합니다.

7

편집 : 만이 게시물을 제출 후 그냥 IdenticalPropertyNamingStrategy를 이용할 수 있도록 유스 케이스가 JMSSerializer 함께 제공 훨씬 쉬울거야, 내가 대신 최우선으로, 발견하고 해제 구성 아래에 설명 된 방법을. 남아있는 무엇, 따라서, 단순히

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

입니다 그것은 당신이 전략을 명명 JMSSerializer의 기본 낙타 케이스를 대체 할 도움이 될 수 있습니다.

// 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)); 
    } 
} 

그런 다음 다음과 같이 당신이 마지막으로 원래

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

대신이 클래스를 사용하는 JMSSerializerBundle을 구성 할 수 있으며, CamelCaseNamingStrategy을 확장 한, 당신은 낮은 케이스 및 구분자 - 삽입을 중지해야 일반적으로 JMSSerializer가 수행합니다. ?

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

멋진!나는 왜 속성 이름이 밑줄을 긋고 그렇게 디폴트로 남아 있지 않는지를 이해하지 못한다. * – dmidz

+0

사실, 그것은 'IdenticalPropertyNamingStrategy'가 당신의 설정 타입과 물건을 고려하지 않기 때문에 쉽지 않다. – dVaffection