Including Entity Metadata and Related Methods Without Dependency in Doctrine - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Friday, May 26, 2023

Including Entity Metadata and Related Methods Without Dependency in Doctrine

I have two Doctrine entities: "Product" and "Highlight". The "Product" entity is part of the "yusrub/catalog" package, while the "Highlight" entity belongs to the "yusrub/highlights" package.

Here is the simplified code for the "Product" entity:

/**
 * Class Product
 * @ORM\Entity(repositoryClass=
 *     "\Yusrub\Catalog\Domain\Repositories\Doctrine\Product\ProductRepository")
 */
class Product 
{
 /**
     * @var  $highlights
     * @ORM\ManyToMany(targetEntity="Yusrub\Highlights\Admin\Domain\Entities\HighLight", inversedBy="products" )
     * @ORM\JoinTable(
     *   name="highlight_product",
     *   joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="entity_id")},
     *   inverseJoinColumns={@ORM\JoinColumn(name="highlight_id", referencedColumnName="id")},
     * )
     */
    protected $highlights;

    /**
     * @ORM\Column(type="integer", name="associated_highlight_count", options={"default":0})
     */
    protected int $associatedHighlightCount;


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

    /**
     * @param HighLight $highlight
     * @return \Yusrub\Catalog\Domain\Entities\Product\Product
     */
    public function addHighlight(HighLight $highlight): Product
    {
        if (!$this->highlights->contains($highlight))
        {
            $this->highlights->add($highlight);
            $highlight->addProduct($this);
        }
        return $this;
    }

    /**
     * @param HighLight $highlight
     * @return Product
     */
    public function removeHighlight(HighLight $highlight): Product
    {
        if ($this->highlights->contains($highlight)) {
            $this->highlights->removeElement($highlight);
            $highlight->removeProduct($this);
        }
        return $this;
    }

    /**
     * @return int
     */
    public function getAssociatedHighlightCount(): int
    {
        return $this->associatedHighlightCount;
    }
}

Currently, the "Product" entity relies on the "Highlight" entity from the "yusrub/highlights" package. However, I want to avoid making the "yusrub/catalog" package dependent on "yusrub/highlights".

Is there a way to include the "Highlight" entity metadata and its related getter and setter methods into the "Product" entity without introducing a package dependency? Alternatively, can I dynamically update the metadata at runtime to achieve this?

Thank you in advance for your help!



source https://stackoverflow.com/questions/76332073/including-entity-metadata-and-related-methods-without-dependency-in-doctrine

No comments:

Post a Comment