我对 FOSRestBundle 和 Doctrine Extensions 有疑问。我Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle
用来实现 Doctrine Extensions。
这是我的问题:我有一个名为“Articles”的实体,它有一些由一些学说扩展提供的字段。在我的情况下,时间戳和可责备。
class Articles {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @var integer
*/
private $id;
/**
* @var string
* @ORM\Column(type="text")
*/
private $description;
/**
* @Gedmo\Blameable(on="create")
* @ORM\ManyToOne(targetEntity="\Zanzibar\UsersBundle\Entity\Users")
* @ORM\JoinColumn(name="createdBy", referencedColumnName="id")
*/
protected $createdBy;
/**
* @Gedmo\Blameable(on="update")
* @ORM\ManyToOne(targetEntity="\Zanzibar\UsersBundle\Entity\Users")
* @ORM\JoinColumn(name="lastEditBy", referencedColumnName="id")
*
* @var Users
*/
private $lastEditBy;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated;
我通过 REST 使用此控制器方法成功创建了新文章:
public function postArticleAction(Request $request)
{
$entity = new Articles();
$form = $this->createForm(new ArticlesType(), $entity);
$form->submit($request);
if ($form->isValid()) {
$entity = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectView(
$this->generateUrl(
'get_article',
array('id' => $entity->getId())
),
Codes::HTTP_CREATED
);
}
return array(
'form' => $form,
);
} //[POST {"description":"a_description"}] /articles
当我创建一个新实体发布类似
{
"description":"test_Article14_",
}
它可以毫无问题地完成工作,给我这样的对象
{
"id":14,
"description":"test_Article14",
"created_by":{
"id":1,
"username":"test",
"username_canonical":"test",
"email":"test@example.com",
"email_canonical":"test@example.com",
"enabled":true,
},
"last_edit_by":{
"id":1,
"username":"test",
"username_canonical":"test",
"email":"test@example.com",
"email_canonical":"test@example.com",
"enabled":true,
},
"created":"2014-01-14T15:44:43+0100",
"updated":"2014-01-14T15:44:43+0100",
}
但是如果我尝试用这个控制器方法修改这篇文章
public function putArticlesAction(Request $request, $id)
{
$entity = $this->getEntity($id);
$form = $this->createForm(new ArticlesType(), $entity);
$form->submit($request);
if ($form->isValid()) {
$entity = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectView(
$this->generateUrl(
'get_article',
array('id' => $entity->getId())
),
Codes::HTTP_CREATED
);
}
return array(
'form' => $form,
);
} //[PUT {"description":"a_description"}] /articles/{id}
我的对象变成
{
"id":14,
"description":"test_Article14_",
"created":"-0001-11-30T00:00:00+0100",
"updated":"-0001-11-30T00:00:00+0100",
}
为了完整起见,我将添加特定于捆绑包的 routing.yml:
articles:
type: rest
resource: Zanzibar\BackendBundle\Controller\ArticlesController
以及涉及我的问题的conf:
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: 'force'
routing_loader:
default_format: json
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
blameable: true
timestampable: true
关于这里的问题有什么想法吗?