serializer icon indicating copy to clipboard operation
serializer copied to clipboard

JMS Deserialize Hydractor

Open elvispdosreis opened this issue 5 years ago • 4 comments

I have doubt to deserealize an element where the parent element makes relationship with the child

Doctrine\DBAL\Exception\NotNullConstraintViolationException: An exception occurred while executing "INSERT INTO produtos_dimensoes (width, height, length, weight, fk_produto) VALUES (?, ?, ?, ?, ?)" with params [90, 222, 85, 146, null]:

image

json

{
  "id": 1220,
  "dimension": [
    {
      "weight": 146,
      "height": 222,
      "width": 90,
      "length": 85
    },
    {
      "weight": 0,
      "height": 142,
      "width": 90,
      "length": 85
    }
  ]
}

Product

<?php
/**
 * Created by PhpStorm.
 * User: elvis
 * Date: 03/02/2019
 * Time: 15:15
 */

namespace App\Domain;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;


/**
 * @ORM\Entity(repositoryClass="ProductRepository")
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Table(name="produtos")
 */
class Product
{
    use TimestampableTrait;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer", name="id_produto")
     * @JMS\Type("int")
     * @JMS\Groups ({"list", "details"})
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Dimension", mappedBy="product", cascade={"persist"})
     * @JMS\Type("array<App\Domain\Dimension>")
     * @JMS\Groups ({"details"})
     */
    private $dimension;

    public function __construct()
    {
        $this->dimension = new ArrayCollection();
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     * @return Product
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

}

Dimension

<?php
/**
 * Created by PhpStorm.
 * User: elvis
 * Date: 12/03/2019
 * Time: 19:33
 */

namespace App\Domain;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * @ORM\Entity(repositoryClass="DimensionRepository")
 * @ORM\Table(name="produtos_dimensoes")
 */
class Dimension
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="id_dimensao", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Type("int")
     * @JMS\Groups ({"details"})
     */
    private $id;

    /**
     * @ORM\Column(type="decimal", precision=10, scale=2, name="width")
     * @JMS\Type("double")
     * @JMS\Groups ({"details"})
     */
    private $width;

    /**
     * @ORM\Column(type="decimal", precision=10, scale=2, name="height")
     * @JMS\Type("double")
     * @JMS\Groups ({"details"})
     */
    private $height;

    /**
     * @ORM\Column(type="decimal", precision=10, scale=2, name="length")
     * @JMS\Type("double")
     * @JMS\Groups ({"details"})
     */
    private $length;

    /**
     * @ORM\Column(type="decimal", precision=10, scale=3, name="weight")
     * @JMS\Type("double")
     * @JMS\Groups ({"details"})
     */
    private $weight;

    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="dimension")
     * @ORM\JoinColumn(name="fk_produto", referencedColumnName="id_produto")
     */
    private $product;

}

elvispdosreis avatar Mar 18 '19 17:03 elvispdosreis

I am still having a hard time doing this deserialization

elvispdosreis avatar Apr 05 '19 11:04 elvispdosreis

Could you please advise me in what way should I do or some guidance?

elvispdosreis avatar Apr 16 '19 23:04 elvispdosreis

To solve the problem I had to create an advisor, would that be the correct way to do it?

Product

/**
* @ORM\OneToMany(targetEntity="Dimension", mappedBy="product", cascade={"persist"})
* @JMS\Type("array<App\Domain\Dimension>")
* @JMS\Groups ({"details"})
* @JMS\Accessor(getter="getDimension", setter="addDimension")
*/
private $dimension;

public function addDimension($arr): self
{
        if(is_array($dimension)){
            foreach ($arr as &$value) {
                $value->setProduct($this);
            }
            $this->dimension = $arr;
        } 
        return $this;
}

Dimension

/**
* @param mixed $product
* @return Dimension
*/
public function setProduct(?Product $product): self
 {
        $this->product = $product;
        return $this;
}

elvispdosreis avatar Apr 23 '19 19:04 elvispdosreis

do you solve this problem? I have some problem too..

Fedchishina avatar Mar 11 '20 14:03 Fedchishina