0

我对 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

关于这里的问题有什么想法吗?

4

2 回答 2

1

耶!!!我解决了!

好的,伙计们,为了将来的参考,我会在这里写下问题所在:是我使用的形式。

我创建了一个带有指定“学说扩展”属性的表单:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('description')
        ->add('created')
        ->add('updated')
        ->add('createdBy')
        ->add('lastEditBy')
    ;
}

当然这不应该去那里,因为它们会自动填满:所以正确的版本是

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('description')
    ;
}

我希望这可以帮助像我一样愚蠢并犯同样错误的人:)

感谢所有花时间解决这个问题的人!

C

于 2014-01-20T14:17:57.903 回答
0

只需将您的 return 语句输入法替换为以下内容,它就会解决您的问题。

return $this->redirectView(
  $this->generateUrl(
    'get_article',
    array('id' => $entity->getId())
    ),
  Codes::HTTP_NO_CONTENT
);
于 2014-01-20T13:31:17.253 回答