Symfony2 Table with 3 entity in Twig

First of all, sorry by advance for my english which is not perfect.

I have a problem for days by reporting 3 doctrine entity in a Twig Table template.

Its a table for manage stocks at work. I have different materials which have each different sizes. Each couple (1 material + 1 size) got a number as amound to order.

So I first created 3 entity :
Stock (represent materials) ManyToMany Dimension (represent sizes)
Besoin (needs) got a ManyToOne relation with both Stock and Dimension.

Then, I created few Stocks, Dimensions and needs with my forms to get a test database.

The goal is now to create a double entry table with the dimensions list, Stocks list and in each cell the needed number.
Its in this step that I have bugs.

For now I can give you my code and hope someone can help me by giving me a tips.

Controller :

public function commandeAction()
    {
        $em = $this->getDoctrine()->getManager();
        $materiauxlist = $em->getRepository(TGComptaBundle:Stock)->findAll();
        $dimensionslist = $em->getRepository(TGComptaBundle:Dimension)->findAll();
        $tab1 = array_merge($materiauxlist, $dimensionslist);
        $besoins = array();

        foreach ($materiauxlist as $stock) {
                foreach ($dimensionslist as $dimension) {
                    $besoin = $em->getRepository(TGComptaBundle:Besoin)->findBesoins($stock, $dimension);
                }
        }

        return $this->render(TGProdBundle:Projet:stocks.html.twig, array(
                materiauxlist => $materiauxlist,
                dimensionslist => $dimensionslist,
                besoin => $besoin));
    }

View :

{% block tgprod_body %}

<div class=well>
    <table class=table table-hover table-bordered>
        <thead>
            <tr>
                <th>#<th>
                    {% for dimension in dimensionslist %}
                        <th>{{ dimension.name }}<th>
                    {% endfor %}
            <tr>
        <thead>
                {% for stock in materiauxlist %}
                    <tr>
                        <td>{{ stock.name }}<td>
                             {% for dimension in dimensionslist %}
                                        {% if besoin %}
                                            <td>{{ besoin.nombre }}<td>
                                        {% else %}
                                            <td>X<td>
                                        {% endif %}
                                {% endfor %} 
                    <tr>
                {% endfor %}
    <table>
<div>

{% endblock %}

Repository :

public function findBesoins($stock, $dimension)
    {
        $qb = $this->createQueryBuilder(b);

        $qb
            ->where(b.stock = :stock)
            ->andwhere(b.dimension = :dimension)
            ->setParameter(stock, $stock)
            ->setParameter(dimension, $dimension);

        $qb
            ->getQuery()
            ->getResult();
    }

My actual problem is : I have X in all cells

View More Answer And Comment : Stackoverflow


Top Answer 1

Before I asked your help I tried :

foreach ($materiauxlist as $stock) {
                foreach ($dimensionslist as $dimension) {
                    $besoin = $em->getRepository(TGComptaBundle:Besoin)->findBesoins($stock, $dimension);
                    $besoins = $besoin;
                }
        }

and :

<table class=table table-hover table-bordered>
        <thead>
            <tr>
                <th>#<th>
                    {% for dimension in dimensionslist %}
                        <th>{{ dimension.name }}<th>
                    {% endfor %}
            <tr>
        <thead>
                {% for stock in materiauxlist %}
                    <tr>
                        <td>{{ stock.name }}<td>
                             {% for dimension in dimensionslist %}
                                        {% for besoin in besoins %}
                                            {% if besoin %}
                                                <td>{{ besoin.nombre }}<td>
                                            {% else %}
                                                <td>X<td>
                                            {% endif %}
                                        {% endfor %}
                                {% endfor %} 
                    <tr>
                {% endfor %}
    <table>

But it was wrong result (I had like 10000 X on each row)
I thinked its the {% for besoin in besoins %} which is wrong, it should be something like {% for besoin.dimension.stock in besoins %} but I cant write this in twig.

and I tried something like this to :

$table = array(stock => $stock, dimension => $dimension, besoin => $besoin);
                    $besoins = $table;

when I do a { dump() } in Twig I have array with number of stock x number of dimension and all with a null besoin so it seems strange and finally I didnt find how to render this array in my table so I let this solution away :/

View More Answer And Comment : Stackoverflow

Unable to implement mpl_connect

I'm working on a GUI that basically hold multiple widgets that each contain a figure as well as a few buttons/whatever. One of the figures is supposed to be interactive, calling a function whenever [Read More...]

What is cURL in PHP?

In PHP, I see the word cURL in many PHP projects. What is it? How does it work? Reference Link: (What is cURL in PHP?) View More Answer And Comment : Stackoverflow cURL is a way you can hit [Read More...]

Add JScrollPane to JPanel

I try to make a scrollbar for a JPanel but when I RUN, the scrollbar not even show up. EDITED: This is the latest code, I updated the contentPane layout. This is main Panel: contentPane = new [Read More...]

c# EventLogQuery using Eventdata

Im trying to determine a machines most frequent user by looking at the security event logs. im looking at using the 4624 Event ID but I cant seem to work out how to add anything from the EventData in [Read More...]

Mock variable in function

For unit testing, I want to mock a variable inside a function, such as: def function_to_test(self): foo = get_complex_data_structure() # Do not test this do_work(foo) # Test this I my [Read More...]