John Davidson

php - Concataine 2 doctrine repository in a single variable

0 comments
Message:


Being a beginner with Symfony and Doctrine I have some difficulties to achieve what I would like.


Indeed I have 2 tables in my database : 'Prints' and 'Files'. I have 2 entities and 2 repositories.


These two tables are similar, we find almost the same variables in the entities.


I made the following code. Of course it works but I don't find it very "professional":


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


This code makes me do a lot of repetitive code like this in my html/twig files :


//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>

A quick and easy solution would be to do this:



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


instead of this :


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

In my controller.


Does anyone have any idea how to do this so easily?


Thank you for your attention and your potential answers.



My Answer:
You can achieve this by using the array_merge() function in PHP to concatenate the results of both repository queries into a single variable. Here's how you can modify your controller code to achieve this:

php
/**
* @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');

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

$owners = $repositoryUser->findAll();
return $this->render('@App/Admin.html.twig', [
'showAll'=> $showAll,
'owners'=> $owners
]);
}


Now in your Twig file, you can loop through the 'showAll' variable to display the data from both 'Prints' and 'Files' repositories.

This way, you can avoid repetitive code and have all the data from both repositories in a single variable.

Rate this post

5 of 5 based on 6378 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us