Symfony原则:获取作为数组持久化的集合



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

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

正如您所看到的,我的模块被持久化为数组。那么,在那之后,我如何(用Symfony(制作Doctrine来获得我的模块

我想你需要一艘ManyToOne关系船。。。据我所知,我们从不在数据库中存储数组。

在您的示例中,订单可以有许多模块,而模块只能有一个订单。。。在这种情况下,称为拥有侧的阶和称为逆侧的模。。。模块保持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
*
* @ORMOneToMany(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
*
* @ORMOneToMany(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;
}
}

当你坚持按原则命令对象时。。。条令处理这个问题并创建

最新更新