Symfony (php) comparison in FindBy statement - php

I am using Symfony and need to compare to variables from my database called $voorraad and $minimumvoorraad. I need to see the product when $voorraad is lower than $minimumvoorraad. Since I am using Symfony we use the PHP language. I've tried the FindByVoorrad and FindOneBy statements with no success, I only get the header from my twig but that's it.
Thanks in advance.

Given your Entity consists of whatever fields including 'voorraad' and 'minimumvoorraad', you should be able to get your database table's content via
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT e
FROM AppBundle:Entity e
WHERE e.voorraad < e.minimumvoorraad'
);
$products = $query->getResult();
Edit: the e in the query is a SQL-typical alias defined inline.
And then treat your $products variable as usual, where you can use all of your getters and setters.
After rendering and passing your products to the Twig view
return $this->render('view.html.twig', array(
'products' => $products
));
you could then proceed to print the products, for example, in a table inside your Twig view:
<table>
{% for product in products %}
<tr>
<td>{{ product.id }}</td>
<td>{{ product.voorraad }}</td>
<td>{{ product.minimumvoorraad }}</td>
</tr>
{% endfor %}
</table>

Related

Can't view collection of data in laravel

i returned a collection of two tables of data with join query as a collection and group by user id. i want to view it on a html table.
My data array looks like this. How can i show this all data into html table view using laravel blade.
if you are sending the collection to your blade template. You can use a foreach loop to iterate through array:
#foreach($items as $item)
<tr>
<td>{{ $item->fname}}</td>
<td>{{ $item->lname }}</td>
</tr>
#endforeach

Laravel Eloquent: Reduce number of queries

Let's say I have 250 users in users table and each user has one or many books, and each book has one or many chapters. Now I would like to print the user names, with their book names.
Controller:
$users = User::all();
in blade:
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>
#foreach($user->books as $book)
{{ $book->name }},
#endforeach
</td>
</tr>
#endforeach
# of queries 252
Now to overcome the n+1 problem, the query should be
$users = User::with('books')->get();
Now the # of queries are only 2.
I want to print the book names with number of chapters like this->
BookName(# of chapters). So in my blade
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>
#foreach($user->books as $book)
{{ $book->name }} ({{ $book->chapters->count() }}),
#endforeach
</td>
</tr>
#endforeach
so for 750 books with 1500 chapters the # of queries are about 752 and it increases if chapter number increases.
Is there any better Eloquent way to reduce it or should I go for raw SQL queries?
You don't need to load all chapters data and then manually count each collection. Use withCount() instead:
$users = User::with('books')->withCount('chapters')->get();
If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models.
From the Eloquent Documentation:
Nested Eager Loading
To eager load nested relationships, you may use "dot" syntax. For example, let's eager load all of the book's authors and all of the author's personal contacts in one Eloquent statement:
$books = App\Book::with('author.contacts')->get();
In your case, you can retrieve the nested relationships you need with the following:
User::with('books.chapters')->get();

Symfony Iterate through objects

I've the following object structure: Order->RepairJobs->Parts. Part has a boolean property InStock. What would be the most efficient way to find out whether a certain Order contains RepairJobs with Parts that are not in Stock? Should I iterate through every RepairJob and then through every Part, or does Symfony/Doctrine have some smart function for this?
In your controller add query builder:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$qb->select('p')
->from('AppBundle:Part', 'p')
->where('p.InStock = false');
$parts = $qb->getQuery()->getResult();
Pass to render Twig template like so:
return $this->render('show/orders_without_stock.html.twig', array(
'parts' => $parts,
));
Then in Twig iterate:
<h2>Orders With Parts Not in Stock</h2>
<table>
<tr>
<th>Order</th>
</tr>
{% for part in parts %}
<tr>
<td>{{ part.getRepairJob.getOrder }}</td>
</tr>
{% endfor %}
</table>
In the above Twig, 'part' is a Part object and 'getRepairJob' is a Part method that gets the Repair Entity. This gets a RepairJob object, then 'getOrder' is the RepairJob method that gets a Order object.
I presume you've setup your Entities and the correct ORM annotations to map each of the Entities. Where both 'RepairJobs' and 'Parts' are collection of objects.
Hopefully this makes sense. But this is how it is done, and it makes things extremely easy to code.

Create small page to display content of the table with Symfony and Doctrine

I am trying to create simple page to display content of the table (customer: id, first_name, last_name) with Symfony + doctrine.
I created GS\OrderBundle\Entity\Customer with columns and getters/setters. Created route for it. I want to create a view , something like:
<table>
{% for c in form %}
{% set id = c.get('value') %}
<tr>
<td>{{ form_widget(c) }}</td>
<td>{{ c[id].firstName }}</td>
<td>{{ c[id].lastName }}</td>
</tr>
{% endfor %}
</table>
and Controller to pass the values from the customer table to this view. I am trying something like:
namespace GS\OrderBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use GS\OrderBundle\Entity\Customer;
use Doctrine\ORM\EntityManager;
class CustomerListController extends Controller
{
public function listAction()
{
$repository = $this->getDoctrine()
->getRepository('GSOrderBundle:Customer');
$customer = $repository->findAll();
$form = $this->createFormBuilder($customer)->getForm();
return $this->render(
'customerTable.html.twig',
array('form' => $form->createView())
);
return new Response($names);
}
}
On the symfony website I found only examples with setting data or outputing single row data. Could you please provide any simple example how to display content of the table in the view?
You have several issues with your code:
Two return statements
Creating a form from an array of customers rather than a single
entity
Not using variable names that you have set in the template
Here is what your view might look like:
<table>
{% for c in customers %}
<tr>
<td>{{ c.id }}</td>
<td>{{ c.firstName }}</td>
<td>{{ c.lastName }}</td>
</tr>
{% endfor %}
</table>
And that's a controller that should do the trick for you:
class CustomerListController extends Controller
{
public function listAction()
{
$repository = $this->getDoctrine()
->getRepository('GSOrderBundle:Customer');
$customers = $repository->findAll();
return $this->render(
'customerTable.html.twig',
array('customers' => $customers)
);
}
}

How to eliminate duplicate values and format the output from DQL in Twig?

I am working on a reporting application in Symfony2, and my twig file shows the following output using DQL. I created an Entity, passed the values using render from action function to the twig. I used a for loop to display all the elements from an associative array (See picture 1). how do I group the result in this specific format? (See picture 2) such that all the duplicate zoneName and rank are eliminated and placed on the top only once.
Current Display of records in twig FROM DQL
The output which I want to achieve
My query in Action function of controller is as follows
$qb->select('distinct t.zoneName, t.rank, t.actSanctList, t.offDate')
) /*, $dql */
->addGroupBy('t.zoneName')
->addGroupBy('t.rank')
->addGroupBy('t.actSanctList')
->addGroupBy('t.offDate')
->orderBy('t.zoneName', 'ASC')
$DL01 = $qb->getQuery()->getResult();
My twig file
{% for DL01_line in DL01_data %}
<tr>
<td>{{ DL01_line.zoneName }}</td>
<td>{{ DL01_line.rank }}</td>
<td>{{ DL01_line.actSanctList }}</td>
<td>{{ DL01_line.offDate|date('Y-m-d') }}</td>
</tr>
{% endfor %}

Categories