【问题标题】:Symfony Doctrine : fetch a collection persisted as arraySymfony Doctrine:获取一个作为数组持久化的集合
【发布时间】:2019-01-05 12:43:46
【问题描述】:

我有两个实体ModulesOrders,其中一个订单有很多模块,我想知道如何获取如下持久化的模块数组集合:

Table: Orders

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

如您所见,我的模块被保存为数组。那么在那之后我怎样才能使 Doctrine(使用 Symfony)来获取我的模块

【问题讨论】:

    标签: php doctrine symfony4


    【解决方案1】:

    我认为您需要一个多对一关系...据我所知,我们从不在数据库中存储数组。

    在您的示例中,订单可以有多个模块,而模块只能有一个订单... 在这种情况下,称为拥有方的顺序和称为反面的模块... 和模块保持订单ID ...

    看看这个例子

    表格:订单

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

    表:模块

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

    你必须这样写你的代码......

    订单类

    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);
            }
        }
    }
    

    模块类

    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;
        }
    }
    

    当您按原则坚持订单对象时...原则处理此问题并创建项目

    【讨论】:

    • 这与我最终采用的方法有点相同。如果我的第一个想法是可能的,那就太好了。无论如何,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多