1

I have two entities Modules and Orders where One order have Many modules and I'm wondering how to fetch an array collection of modules persisted as follow:

Table: Orders

id | modules | user_id | ... | created_at          |
----------------------------------------------------
1  | [2,6,5] | 12      | ... | 2018-07-28 00:00:00 |
----------------------------------------------------

As you can see my modules are persisted as array. So after that how can I make Doctrine (with Symfony) to get my modules

1 Answer 1

2

I think you need a ManyToOne relationShip ... as I know, we never store an array in database.

in your example order can have many modules and module can have just one order ... in this case order called owning side and module called invers side ... and module keep id of order ...

look at this example

Table: Orders

id | user_id | ... | created_at          |
----------------------------------------------------
1  | 12      | ... | 2018-07-28 00:00:00 |
----------------------------------------------------

Table: Modules

id | order_id | ... | created_at          |
----------------------------------------------------
1  | 1        | ... | 2018-07-28 00:00:00 |
----------------------------------------------------
2  | 1        | ... | 2018-07-29 00:00:00 |
----------------------------------------------------

you must write your code like this...

Order Class

class Order implements OrderInterface
{

     /**
     * @var Collection
     *
     * @ORM\OneToMany(targetEntity="Module", mappedBy="order", cascade={"persist"})
     */
    protected $modules;

     /**
     * Don't forget initial your collection property 
     * Order constructor.
     */
    public function __construct()
    {
        $this->modules = new ArrayCollection();
    }

        /**
     * @return Collection
     */
    public function getModules(): Collection
    {
        return $this->modules;
    }

    /**
     * @param ModuleInterface $module
     */
    public function addModule(ModuleInterface $module): void
    {
        if ($this->getModules()->contains($module)) {

            return;
        } else {

            $this->getModules()->add($module);
            $module->setOrder($this);
        }
    }

    /**
     * @param ModuleInterface $module
     */
    public function removeModule(ModuleInterface $module): void
    {
        if (!$this->getModules()->contains($module)) {

            return;
        } else {

            $this->getModules()->removeElement($module);
            $module->removeOrder($this);
        }
    }
}

Module Class

class Module implements ModuleInterface
{
    /**
     * @var OrderInterface
     *
     * @ORM\OneToMany(targetEntity="Order", mappedBy="modules", cascade={"persist"})
     */
    protected $order;

    /**
    * @param OrderInterface $order
    */
    public function setOrder(OrderInterface $order) 
    {

        $this->order = order;
    }

    public function getOrder(): OrderInterface
    {
        return $this->order;
    }
}

when you persist order object by doctrine... doctrine handle this and create items

Sign up to request clarification or add additional context in comments.

1 Comment

it's a bit the same approach I adopted finaly. It would've been realy nice if my first thought was possible. Anyway, thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.