how to separate a row result to grups in TWIG - symfony

Form query i have several rows record
Controller:
public function statusTaskAction()
{
$em = $this->getDoctrine()->getManager();
$statusAll = $em->getRepository( 'MyBundle:StatusTask' )
->allStatusQuery();
return [ 'rows' => $statusAll ];
}
statusTask.html.twig
<table class="table ">
<tbody>
{% for entry in row %}
<tr>
<td>{{ entry.endDate is null ? '' : entry.endDate }}</td>
<td>{{ entry.groupID }}</td>
<td>{{ entry.name }}</td>
<td>{{ entry.info }}</td>
</tr>
{% else %}
<tr>
<td colspan="4">No rows</td>
</tr>
{% endfor %}
</tbody>
</table>
groupID displayed status:
g - "Green"
r - "Red"
f - "Future to plan"
p - "Planed"
I'll try to separate result by groupID and for this result add label, decription to each displayed grupID in TWIG and separate it in TWIG.
How can I do it in TWIG?

Since row is an Entity, you don't call the column name, but instead the Entity getters like so:
<table class="table ">
<tbody>
{% for entry in rows %}
<tr>
<td>{{ entry.getDate is null ? '' : entry.getDate }}</td>
<td>{{ entry.getGroupID }}</td>
<td>{{ entry.getName }}</td>
<td>{{ entry.getInfo }}</td>
</tr>
{% else %}
<tr>
<td colspan="4">No rows</td>
</tr>
{% endfor %}
</tbody>
</table>
I presume that would be the naming in your Entity class for the Entity StatuTask. Also, I think you need to use rows as specified in your controller.

Related

Echo out from a Laravel 8 Eloquent collection

I have this collection
How can I echo the relations in Blade? I try this code:
#forelse($articles as $article)
<tr>
<td>{{ $article->title }}</td>
<td>{{ $article->excerpt }}</td>
<td>{{ $article->body }}</td>
<td>{{ $article->colors->name }}</td>
...
But I get: "Trying to get property 'name' of non-object (View: C:\xampp\htdocs\laravel\resources\views\articles\index.blade.php)"
You have a Collection (albeit of only a single Color), so you'll need either a nested #foreach(), or a pluck():
#forelse($articles as $article)
<tr>
<td>{{ $article->title }}</td>
...
#foreach($article->colors as $color)
// Do something with `$color->name`
#endforeach
</tr>
#empty
...
#endforelse
// OR
#forelse($articles as $article)
<tr>
<td>{{ $article->title }}</td>
...
<td>{{ $article->colors->pluck('name')->implode(', ') }}</td> // Purple
</tr>
#empty
...
#endforelse
Basically, you can't echo a single property (like ->name) from a Collection. The code won't know which one you're trying to return. This applies even if the relationship returns a single record. If you only want a single color, then you'd need to modify your relationship to as hasOne(), then you'd be able to do something like:
$article->color->name ?? 'None'

How to get the number of result? - Pagerfanta and Twig (in Symfony)

In my Demo application, use Pagerfanta to read only a few rows from Database.
public function findAllProductsByCategory($category, int $page = 1): Pagerfanta
{
// DQL = Always select full classes from classes
$query = $this->getEntityManager()
->createQuery('
SELECT p
FROM App\Entity\Product p
WHERE p.category = :category
')
->setParameter('category', $category);
return $this->createPaginator($query, $page);
}
Later in Twig, I loop the preselected results. Everything is fine ...
{% if products.haveToPaginate %}
<div class="navigation text-center">
{{ pagerfanta(products, 'default', {routeName: 'category_show_paginated', 'routeParams' : { 'id': category.id}}) }}
</div>
{% endif %}
<table border="1" cellpadding="5">
<tr>
<th>#</th>
<th>Name</th>
<th>Products</th>
<th>Description</th>
</tr>
{% for product in products %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ product.name }}</td>
<td>{{ product.description }}</td>
</tr>
{% endfor %}
</table>
but on each page, "loop.index" is the same 1,2,3,4 ...
I would like to show the number of results 41,42,43 ... with pagerfanta ;)
Is there a special function?
Thank you
Haven't worked with Pagerfanta for a while, but what you want should be doable with something like this:
{{ pagerfanta.currentPageOffsetStart() + loop.index }}
Example test case: https://hotexamples.com/examples/pagerfanta/Pagerfanta/getCurrentPageOffsetStart/php-pagerfanta-getcurrentpageoffsetstart-method-examples.html

Symfony2 FOSUserBundle Display expiresAt in twig template

I want to display the expiresAt in my twig but i have this error :
Method "expiresat" for object "AuthenticationBundle\Entity\User" does not exist in AdminNCBundle:NC:list_clients.html.twig at line 43
in my twig i have done a for :
{% for client in clients %}
<tr>
<td>{{ client.firstname }} {{ client.lastname }}</td>
<td>{{ client.telephone }}</td>
<td>{{ client.registrationdate|date('Y-m-d H:i:s') }}</td>
<td>{{ client.expiresat|date('Y-m-d H:i:s') }}</td>
<td>{{ client.duration }}</td>
</tr>
{% endfor %}
the registrationdate displayed very well.
the answer of geoB is correct
i added a method in my User entity
public function getExpiresAt() {
return $this->expiresAt;
}
because FOSUserBundle Model User dosen't has a getExpiresAt method

Symfony2, doctrine2, can I return an array as a response in the controller?

I've got an embedded controller that takes in a parameter and retrieves from the db.
{% for key, value in boxes %}
{{ render(controller('ABundle:Reports:getBox', { 'boxnum' : value.boxnum })) }}
<tr>
<td>{{ value.boxnum }}</td>
<td>{{ value.boxname }}</td>
<td>( # of total box )</td>
<td>( # of available box)</td>
</tr>
{% endfor %}
Controller:
public function getBox($boxnum) {
$em = $this->getDoctrine()->getRepository('ABundle:MainBoxes');
$getbox = $em->availBoxes($boxnum);
return new Response(array('getbox' => $getbox)); // this doesn't work obviously...
}
I want the results to retrieve db and print it out in a loop. Something like
<tr>
<td>{{ value.boxnum }}</td>
<td>{{ value.boxname }}</td>
<td>{{ getbox.totalbox }}</td>
<td>{{ getbox.availbox }}</td>
</tr>
I'm getting an error for the wrong response return. It's not allowing me to return an array?
An exception has been thrown during the rendering of a template ("The Response
content must be a string or object implementing __toString(), "array" given.")

An attribute is not displayed in twig

I have an attribute immobilier.ref that is not displayed with twig
this is the code
{% for immobilier in listImmobilier %}
<tr>
<td>{{ immobilier.id }}</td>
<td>{{ immobilier.ref }}</td>
<td>{{ immobilier.titre }}</td>
<td><span class="glyphicon glyphicon-eye-open" style="display:block; text-align:center"></span></td>
<td>Edit</td>
<td>Delete</td>
</tr>
{% endfor %}
this is a photo of the table
It(s fine now, it was because I have a function named ref in the entity
/**
* #ORM\PostPersist()
*/
public function ref()
{
$id = $this->getId();
$operation = $this->getOperation()->getRef();
$this->setRef($id.$operation);
}
I have changed her name and it work fine now.

Resources