Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Problem with POST and @ORM\GeneratedValue(strategy="NONE") #17

Open
AmsTaFFix opened this issue Nov 7, 2013 · 1 comment
Open

Problem with POST and @ORM\GeneratedValue(strategy="NONE") #17

AmsTaFFix opened this issue Nov 7, 2013 · 1 comment

Comments

@AmsTaFFix
Copy link
Collaborator

Sorry for my english.
Okey, what i have.
First i want to say, that entity's creation with AUTO generate strategy works perfect

Entity

<?php

namespace Billing\Bundle\MiscBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Tpg\ExtjsBundle\Annotation as Extjs;
use JMS\Serializer\Annotation as JMS;

/**
 * TCode
 *
 * ....
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class TCode
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="bigint")
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\Id
     * @JMS\Groups({"All","Default"})
     */
    private $id;
....

Controller

    public function postTcodesAction() {
        $serializer = $this->get("tpg_extjs.orm_serializer");
        $entity = $serializer->deserialize(
            $this->getRequest()->getContent(),
            'Billing\Bundle\MiscBundle\Entity\TCode',
            'json',
            DeserializationContext::create()->setGroups(array("Default", "post"))
        );
        $validator = $this->get('validator');
        $validations = $validator->validate($entity, array('Default', 'post'));

GET, PATCH operarations works correct, but when i trey to create new entity i have erorr:

"ReflectionProperty::setValue() expects parameter 1 to be object, null given"
Tpg\ExtjsBundle\Component\JsonDeserializationVisitor.php line 45

class JsonDeserializationVisitor extends Base {
    public function visitProperty(PropertyMetadata $metadata, $data, Context $context) {
...
                $metadata->reflection->setValue($this->getCurrentObject(), $v); // it's line 45
...

after debugging, i found, that calling "$this->objectConstructor->construct" in JMS/Serializer/GraphNavigator::accept

...
                $object = $data;
                if ($context instanceof DeserializationContext) {
                    $object = $this->objectConstructor->construct($visitor, $metadata, $data, $type);
                }
...
                $visitor->startVisitingObject($metadata, $object, $type, $context);
...

returns null.

How i fix it

before call deserialize, i create new entity and persist it in Manager

...
public function postTcodesAction() {
        $manager = $this->get('doctrine.orm.default_entity_manager');
        $newEntity = new TCode();
        $obj = json_decode($this->getRequest()->getContent());
        $newEntity->setId($obj->id);
        $manager->persist($newEntity);

        $serializer = $this->get("tpg_extjs.orm_serializer");
        $entity = $serializer->deserialize(
            $this->getRequest()->getContent(),
            'Billing\Bundle\MiscBundle\Entity\TCode',
            'json',
            DeserializationContext::create()->setGroups(array("Default", "post"))
        );
...

it's very quick fix, but it's works for me.
Can you tell me, maybe i have mistake somewhere?

@AmsTaFFix
Copy link
Collaborator Author

or we can delete from $this->getRequest()->getContent() id property, then deserialize, then set id property to entity, then persist and it will work

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant