【发布时间】:2015-04-30 19:17:58
【问题描述】:
我正在开发一个 API 并拥有以下实体:
/**
* Solicitud de registro.
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="DnD\RaHApiBundle\Repository\ApplicationRepository")
*/
class Application
{
public function __construct()
{
$pending = new Event("PENDING", "APPLICATION");
$this->status = new ArrayCollection($pending);
$this->ownsVehicle = false;
$this->resident = true;
$this->timestamp = new DateTime();
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"application_info"})
*/
private $id;
...
注意构造函数。它使用默认值设置我的实体中的一些字段。现在我的控制器的操作:
/**
* Crea una nueva solicitud de afiliacion
* @return array
*
* @Post("applications")
* @ParamConverter("application", converter="fos_rest.request_body")
*/
public function postAction(Application $application)
{
$this->applicationsRepo->save($application);
// It has no date, no status, no defaults!!!
$view = $this->view($application, 200);
$view->getSerializationContext()->setSerializeNull(true);
$view->getSerializationContext()->setGroups(array('application_info'));
return $this->viewHandler->handle($view);
}
$application 对象没有设置默认值,这是为什么呢?如何让转换器调用构造函数并设置所有默认值?
【问题讨论】:
标签: php symfony converter fosrestbundle