[英]FOSRestBundle Doctrine extension not working on “PUT” method
I have a problem with FOSRestBundle and Doctrine Extensions. 我对FOSRestBundle和Doctrine Extensions有问题。 I use
Stof\\DoctrineExtensionsBundle\\StofDoctrineExtensionsBundle
to implement Doctrine Extensions. 我使用
Stof\\DoctrineExtensionsBundle\\StofDoctrineExtensionsBundle
来实现Doctrine Extensions。
Here my problem: I have an Entity called "Articles" that has some field feeded by some doctrine extensions. 这是我的问题:我有一个称为“文章”的实体,该实体的某些领域由某些学说扩展提供。 In my case Timestampable and Blameable.
就我而言,是时间戳和可责的。
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;
I'm succesfull in creating new Articles via REST with this controller method: 我可以使用此控制器方法通过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
when i create a new entity posting something like 当我创建一个新的实体发布类似
{
"description":"test_Article14_",
}
it does its work without problems, giving me object like 它的工作没有问题,给了我类似的对象
{
"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",
}
but if I try to modify this Article with this controller method 但是如果我尝试使用此控制器方法修改本文
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}
my object become 我的对象变成
{
"id":14,
"description":"test_Article14_",
"created":"-0001-11-30T00:00:00+0100",
"updated":"-0001-11-30T00:00:00+0100",
}
for completeness i'll add the bundle specific routing.yml: 为了完整性,我将添加捆绑包特定的routing.yml:
articles:
type: rest
resource: Zanzibar\BackendBundle\Controller\ArticlesController
and the conf that refer to my problem: 和提到我的问题的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
Any Idea on what the matter is here? 对这件事有什么想法吗?
Yay!!! 好极了!!! I solved!
我解决了!
ok guys, for future references I'll write down here which the problem was: was into the form I used. 好的,伙计们,我将来在这里写下问题所在:问题在于使用的表格。
I created a form with the "doctrine extended" attributes specified: 我使用指定的“扩展的教义”属性创建了一个表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description')
->add('created')
->add('updated')
->add('createdBy')
->add('lastEditBy')
;
}
of course this should not go there, as they are automatically filled up: so the correct version is 当然这不应该去那里,因为它们会自动填满:因此正确的版本是
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description')
;
}
I hope this could help someone being as dumb as me and doing my same error :) 我希望这可以帮助某人像我一样愚蠢并做同样的错误:)
thanks to all the ones spent their time to solve this! 感谢所有花费时间解决此问题的人!
C C
Just replace your return statement input method with following, it would resolve your problem. 只需将您的return语句输入方法替换为以下内容,即可解决您的问题。
return $this->redirectView(
$this->generateUrl(
'get_article',
array('id' => $entity->getId())
),
Codes::HTTP_NO_CONTENT
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.