在单个变量中连接2原则存储库



使用Symfony教义和为了达到我想要的,我遇到了一些困难。

确实我有2表在我的数据库:'打印'和'文件'。我有两个实体和两个存储库。

这两个表是相似的,我们在实体中发现几乎相同的变量。

我编写了以下代码。当然可以,但是我不觉得它很"专业":

//we are in a new controller 

/**
* @Route("/Admin/", name="Admin")
*/
public function AdminAction(){
$doctrine=$this->getDoctrine();
$repositoryPrints =$doctrine->getRepository('AppBundle:Prints'); 
$repositoryFiles =$doctrine->getRepository('AppBundle:Files'); 

$repositoryUser =$doctrine->getRepository('AppBundle:User'); 
$showPrints = $repositoryPrints->findAll();
$showFiles = $repositoryFiles->findAll();
$owners = $repositoryUser->findAll();
return $this->render('@App/Admin.html.twig', [
'showPrints'=> $showPrints,
'showFiles'=> $showFiles,
'owners'=> $owners
]);
}

这段代码让我在我的html/twig文件中做了很多重复的代码:

//The Admin.html.twig file
{% extends 'base.html.twig' %}
{% block body %}
<table class="tableClass">
<thead>
<tr>
<th class="head">Tilte</th>
<th class="head">Owner</th>
<th class="head">Size</th>
<th class="head">Printing Duration</th>
<th class="head">Due Date</th>
<th class="headStatus">Status</th>
{% set break = false %}
{% for element in showPrints %}
{% if element.done == 1 and break == false %}
<th class="head">
Date of print
</th>     
{% set break = true %}
{% endif %}
{% endfor %}
{% for element in showFiles %}
{% if element.done == 1 and break == false %}
<th class="head">
Date of print
</th>     
{% set break = true %}
{% endif %}
{% endfor %} 
</tr>
</thead>

一个快速而简单的解决方案是这样做:


$showAll = $repositoryPrints->findAll() + $repositoryFiles->findAll();

而不是:

$showPrints = $repositoryPrints->findAll();
$showFiles = $repositoryFiles->findAll();

In my controller.

有谁知道如何做到这一点这么容易吗?

谢谢你的关注和你可能的答案。

findAll()函数将返回数组。我认为你可以用array_merge函数合并。

$prints = $repositoryPrints->findAll();
$files = $repositoryFiles->findAll();
$showAll = array_merge($prints, $files);

最新更新