Search code examples
mongodbdatabase-designdatabase-schemaapi-platform.comdoctrine-odm

Doctrine ODM schema for E-Commerce


I'm beginner and would like to gain knowledge about nosql, mongodb, document structure. I tried to make documents for e-commerce platform. I have a product.json and mapping documents due to that but I'm stuck about doctrine odm reference relations. My codes are not working.

The structure I want

    Product 
{
    id: 
    name:
    price: [{price1, discount2}, {price2, discount2}, ...]
    images: [{img1,timg1}, {img2,tim2}]
    comments: {[id, username, comment], ...}
    shop: [{shopinfo...}]

}

So, I think the price, images and comments should be embedded document. Shop, product has one to one relationship right? I'm coding this with doctrine odm reference in api-platform with mongodb. Here is my codes.


Solution

  • I did it with using doctrine odm referations. To create references we have to create variables to interact classes between each other. For instance, in Product class

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

    In Price class

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

    In api-platform references can be using by iri references.