LerochkaKapito / extjs-bundle

Use ExtJs with Symfony 2
MIT License
21 stars 18 forks source link

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

Open AmsTaFFix opened 10 years ago

AmsTaFFix commented 10 years ago

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 commented 10 years ago

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