Question

I have a problem with FOSRestBundle and Doctrine Extensions. I use Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundleto implement 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:

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:

articles:
type: rest
resource: Zanzibar\BackendBundle\Controller\ArticlesController

and the conf that refer to my problem:

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?

Was it helpful?

Solution

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

OTHER TIPS

Just replace your return statement input method with following, it would resolve your problem.

return $this->redirectView(
  $this->generateUrl(
    'get_article',
    array('id' => $entity->getId())
    ),
  Codes::HTTP_NO_CONTENT
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top