mongodb-queryapi-platform.comdoctrine-odmdatabase-relations

Api-Platform ODM IRI reference gets empty objects


I want to use Product and Price classes with one-to-many references. This is my Product class.

    /**
     * @ApiResource
     *
     * @Document
     */
    class Product
    {

        /**
         * @ODM\Id(strategy="INCREMENT", type="integer")
         */
        private $id;

        /**
         * @ODM\Field(type="string")
         * @Assert\NotBlank
         */
        public $name;

        /**
         * @ODM\ReferenceMany(targetDocument=Price::class, mappedBy="product", cascade={"all"}, storeAs="id")
         */
        public $prices ;

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

        }
//getter and setter of id...
    }

This is Price class

/**
 * @ApiResource
 *
 * @ODM\Document
 */
class Price
{

    /**
     * @ODM\Id(strategy="INCREMENT", type="integer")
     */
    private $id;


    /**
     * @ODM\Field(type="float")
     * @Assert\NotBlank
     * @Assert\Range(min=0, minMessage="The price must be superior to 0.")
     * @Assert\Type(type="float")
     */
    public $price;

    /**
     * @Assert\Type(type="integer")
     */
    private $discount;

    /**
     * @ODM\ReferenceOne(targetDocument=Product::class, inversedBy="prices", storeAs="id")
     */
    public $product;

I can put and get Price with id=1 but when I want to put Product in swagger ui I use this reference.

{
  "name": "productno1",
  "prices": [
    "/api/prices/1/"
  ]
}

It gives 201. I can check the db it is stored. The name of the product is productno1 but price section is empty. Can anyone help about what is wrong?


Solution

  • I changed mappedby tag of prices variable in Product class with inversedby, then problem solved.