0

im working with on a project with Symfony + doctrine-mongodb-odm + MongoDB. I have two classes Product and productCategory as shown here :

<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

#[MongoDB\Document]
class Product
{
    #[MongoDB\Id]
    private string $id;
    private string $title;

    #[MongoDB\EmbedOne(targetDocument: ProductCategory::class)]
    private ProductCategory $productCategory;

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

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

        return $this;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param mixed $title
     *
     * @return Product
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * @return ProductCategory
     */
    public function getProductCategory(): ProductCategory
    {
        return $this->productCategory;
    }

    /**
     * @param ProductCategory $productCategory
     *
     * @return Product
     */
    public function setProductCategory(ProductCategory $productCategory): Product
    {
        $this->productCategory = $productCategory;

        return $this;
    }


}

<?php

namespace App\Document;

class ProductCategory
{
    private $id;
    private $content;

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

    /**
     * @param mixed $id
     *
     * @return ProductCategory
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * @param mixed $content
     *
     * @return ProductCategory
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }


}

I want to store the ProductCategory list in a separate collection, i written in the doc here https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/reference/annotations-reference.html#embeddeddocument we should not use an object from other collection as an embedded document , in this case i have to create an other model "ProductCategoryOther" to deal with this requirement so whats the good choice to do this ?

Thanks.

i try to embed a ProductCategory object into Product Object and its work so even its work , is this a good or a bad practice ?

Aymen
  • 13
  • 4

0 Answers0