sorting with KnpPaginator - symfony

I have an issue trying to make my sorting with KnpPaginator works. I followed many links and try a lot of iteration in my code, it can't seem to work properly. i'm missing something but can't locate it
The only thing that works at the moment si the table headers where i can click on it but nothing happens except refreshing the page and refreshing me back on pagination 1.
I'm working on symfony 2.3
Here is my bundle configuration
knp_paginator:
page_range: 5 # default page range used in pagination control
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: true # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
pagination: 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig' # sliding pagination controls template
sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template
Here is my controller where I actually setted up Knp with the getRepository
private function resultsAction(Request $request, User $user, $type, $archive)
{
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive);
$paginator = $this->get('knp_paginator');
$results = $paginator->paginate(
$query,
$request->query->getInt('page',1),
$request->query->getInt('limit',50)
);
return array("results" => $results, "archive" => $archive);
}
public function offreAction(Request $request, User $user, $archive = false)
{
return $this->resultsAction($request, $user, Operation::OFFRE_COMMERCIALE, $archive);
}
here is my repo where the query is done:
public function getQueryByTypeAndPro($type, User $user, $archive)
{
return $this->createQueryBuilder("opn")
->andWhere("opn.type = :type")
->setParameter("type", $type)
->andWhere("opn.resellerId = :reseller")
->setParameter("reseller", $user->getId())
->andWhere("opn.archive = :archive")
->setParameter('archive', $archive)
->orderBy("opn.dateCreation", "DESC")
->getQuery()
;
}
And here is my view where I try to make things work with Knp
<tr>
<th>{{ knp_pagination_sortable(results, 'general.vehicule.ref'|trans, 'opn.type') }}</th>
<th>{{ knp_pagination_sortable(results, 'general.vehicule.vehicule'|trans, 'opn.resellerId') }}</th>
<th>{{ knp_pagination_sortable(results, 'notifications.client'|trans, 'opn.??') }}</th>
<th>{{ knp_pagination_sortable(results, 'general.date'|trans, 'opn.??') }}</th>
<th>{{ knp_pagination_sortable(results, 'commerce.achat.encours.etat'|trans, 'opn.??') }}</th>
</tr>
And in my table, when I click on it refresh the page but doesn't sort it. I'm having a hard time understand how to make that work?
The place where I putted 'opn.??' its because I don't know what to put at that particular place, I don't seem to understand the query
I would like the last dated item to be first, but being able to sort it with Knp

Okay i succeeded in making that work, here is what I've done.
config.yml
knp_paginator:
page_range: 5 # default page range used in pagination control
default_options:
page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name
sort_direction_name: direction # sort direction query parameter name
distinct: false # ensure distinct results, useful when ORM queries are using GROUP BY statements
template:
pagination: 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig' # sliding pagination controls template
sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template
repo
public function getQueryByTypeAndPro($type, User $user, $archive)
{
return $this->createQueryBuilder("opn")
->andWhere("opn.type = :type")
->setParameter("type", $type)
->andWhere("opn.resellerId = :reseller")
->setParameter("reseller", $user->getId())
->andWhere("opn.archive = :archive")
->setParameter('archive', $archive)
->orderBy("opn.dateCreation", "DESC")
->getQuery()
;
}
controller
private function resultsAction(Request $request, User $user, $type, $archive)
{
$em = $this->getDoctrine()->getManager();
$paginator = $this->get('knp_paginator');
$qb = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive);
$results = $paginator->paginate(
$qb,
$request->query->get('page',1),
$request->query->get('limit',50),
[
'defaultSortFieldName' => 'opn.dateCreation',
'defaultSortDirection' => 'desc'
]
);
return array("results" => $results, "archive" => $archive);
}
and twig
<tr>
<th{% if results.isSorted('opn.id') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.vehicule.ref'|trans, 'opn.id') }}</th>
<th{% if results.isSorted('opn.vehiculeMarque') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.vehicule.vehicule'|trans, 'opn.vehiculeMarque') }}</th>
{% if typeOffre is defined and typeOffre == 'devisWeb' %}<th>Financement</th>{% endif %}
<th{% if results.isSorted('opn.clientNom') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'notifications.client'|trans, 'opn.clientNom') }}</th>
<th{% if results.isSorted('opn.dateCreation') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(results, 'general.date'|trans, 'opn.dateCreation') }}</th>
<th>{{ 'commerce.achat.encours.etat'|trans }}</th>
<th class="sorting_disabled">{{ 'commerce.achat.action'|trans }}</th>
<th class="sorting_disabled">{{ 'commerce.vente.operation.basculer'|trans }}</th>
</tr>
and so that works

remove ->orderBy("opn.dateCreation", "DESC"). KnpPaginator have special Listener and Walker that can modify your query with correct ORDER BY based on request and then eqecute an paginate it for you.
Config sortable look at here.
Translation look at here.

Related

Show multiple data from an Entity

I want to show the best 5 scores from each different Game I have. So I made this function :
public function records (){
$em = $this->getDoctrine()->getManager();
$games = $em->getRepository(Game::class)->findAll();
foreach($games as $g){
$records = new ArrayCollection;
$records = $em->getRepository(Game::class)->findAllRecords($g->getId());
}
return $this->render('game/records.html.twig', [
'games' => $games,
'records' => $records,
]);
}
Here is the repository function :
public function findAllRecords($id){
$qb = $this->createQueryBuilder('g');
$qb->select('g.name')
->innerJoin('g.Parties', 'p')
->innerJoin('p.playeds', 'y')
->innerJoin('y.joueur', 'j')
->addSelect('y.score')
->addSelect('j.nom, j.prenom')
->where('g.id = :id')
->setParameter('id', $id)
->orderBy('y.score', 'DESC')
->setMaxResults('5');
var_dump($qb->getDQL());
$query = $qb->getQuery();
return $query->getResult();
}
And finally the view :
{% for g in games %}
{{ g.name }}
<table class="table">
<tbody>
<tr>
<th>score</th>
</tr>
{% for r in records %}
<tr>
<td>{{ r.score }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
It doesn't completely works as I just get the data from the last game ID. How can I show the data for each game ?
foreach($games as $g){
$records = new ArrayCollection;
$records = $em->getRepository(Game::class)->findAllRecords($g->getId());
}
Here's your issue. This is always overwriting. You want to do something like:
$records = new ArrayCollection;
foreach($games as $g) {
$records[] = $em->......;
}
That should solve your issue

Symfony FOSUserBundle Role array

I use This function to get data
public function UserAction()
{
$easyuser = $this->getDoctrine()->getrepository('AppBundle:User')->findall();
foreach($easyuser as $user){
$id = $user->getid();
$username = $user->getUsername();
$email = $user->getEmail();
$roles = $user->getRoles();
}
return $this->render('easycall/user.html.twig', ['easyuser' => $easyuser, 'roles' => $roles]);
}
and in twig i use this code to show data
{% for entity in easyuser %}
<tr>
<td>{{entity.id}}</td>
<td>{{entity.username}}</td>
<td>{{entity.email}}</td>
{% for role in entity.roles %}
<td>{{role}}</td>
{% endfor %}
</tr>
{% endfor %}
The problem is that i get all the roles if the user is ROLE_SUPER_ADMIN, i want to get only the first value from every array.
i tried something like reset() but it did'nt work, any suggestion??
This is also a picture how the results look likes.
1st item from array shoul be somethink like
{{entity.roles | first}}
but it's simply 1st item from array, i'm not sure if it always be more "powerfull role"

egeloen/ivory-google-map load only one map

I am using egeloen/ivory-google-map library to show map for each registered user. But in all user list only one map is showing.
This is my method:
public function findCompaniesAction() {
$em = $this->getDoctrine()->getManager();
$companies = $em->createQueryBuilder()
->select('b')
->from('RFQIronilBundle:User', 'b')
->where('b.acc_type = 1')
->getQuery()
->getResult();
foreach ($companies as $key => $company) {
$map = $this->get('ivory_google_map.map');
$map->setMapOption('mapTypeId', MapTypeId::ROADMAP);
$map->setMapOption('mapTypeId', 'roadmap');
$company->map = $map;
}
return $this->render('RFQIronilBundle:Default:find-companies.html.twig', array(
'companies' => $companies
));
}
and in my view I use:
{% for companies in companies %}
{{ google_map_container(companies.map) }}
{{ google_map_js(companies.map) }}
{% endfor %}
All is fine, but map is showing for only first user. What I have missed?
Try giving each map a unique container id. I use something like this:
$map->setHtmlContainerId('map_canvas_'.md5($address));
where $address is a simple address string.

Get value from collection field in Twig

I have collection form and need to access the value to show in my view. The problem is seems the key variable declared as Integer and I got error like this :
Impossible to access an attribute ("nama") on a integer variable ("0")
in SifoAdminBundle:DftAbsensi:manage.html.twig at line 65
Here my Twig :
{% for key, absensi in form_edit %}
<li>{{ form_edit.vars.value.statusS.key.nama }}</li>
{% endfor %}
If I change {{ form_edit.vars.value.statusS.key.nama }} into {{ form_edit.vars.value.statusS.1.nama }} its works fine.
Here my controller :
/* Show data */
$emShow = $this->getDoctrine()->getManager();
$collectionAbsensi = new CollectionAbsensi();
foreach ($entityGrupPelajar as $temp) {
$entity = new DftAbsensi();
$entity = $emShow->getRepository('SifoAdminBundle:DftAbsensi')->findOneBy(array('idGrupPelajar' => $temp, 'tanggal' => $tanggal));
if ($entity)
{
$entityPelajar = $emShow->getRepository('SifoAdminBundle:MstPelajar')->find($temp->getIdPelajar());
$dftAbsensi = new DftAbsensi();
$dftAbsensi->setId($entity->getId())
->setIdGrupPelajar($entity->getIdGrupPelajar())
->setTanggal($entity->getTanggal())
->setStatus($entity->getStatus())
->setNis($entityPelajar->getNis())
->setNama($entityPelajar->getNama())
;
$collectionAbsensi->getStatusS()->add($dftAbsensi);
}
}
$emShow->flush();
$formEdit = $this->createForm(new CollectionAbsensiType(), $collectionAbsensi);
$formEdit->add('save', 'submit', array('attr' => array('class' => 'btn btn-info')));
return $this->render('SifoAdminBundle:DftAbsensi:manage.html.twig', array(
'form_edit' => $formEdit->createView(),
));
I have searched for this problem also read the issue #902 but still this problems occurs in my Symfony 2.4 on PHP 5.4
Is there any ways to get that value in iteration without key?
Try changing
form_edit.vars.value.statusS.key.nama
To
form_edit.vars.value.statusS[key].nama
The former is equivalent to $form_edit['vars']['value']['statusS']['key']['nama'] (which does not exist) while the latter is equivalent to $form_edit['vars']['value']['statusS'][$key]['nama']

symfony trying to export a csv file from a link

I have this function in the controller, that returns a form with a select box. When the values are selected, I retrieve them with
$manifestations = $form['manifestations']->getData();
then put them in a repository function that queries the database $invites = $repository->searchInviteByManif($manifestations);
public function indexAction() {
$entity = new Invite();
$form = $this->createForm(new ManifSearchType(), $entity);
$request = $this->get('request');
$invites = null;
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
$message = '';
$manifestations = $form['manifestations']->getData();
$repository = $this->getDoctrine()
->getManager()
->getRepository('PrifProtocoleBundle:Invite');
$invites = $repository->searchInviteByManif($manifestations);
$response1 = $this->render('PrifProtocoleBundle:Invite:index.html.twig', array(
'form' => $form->createView(),
'entities' => $invites,
'message' => $message,
));
return $response1;
}
return array(
'form' => $form->createView(),
'entities' => $invites,
);
}
This function then returns a view index.html.twig with a table and all the fields found in the db.
What I want is to export all the queried data $invites in a CSV file, by clicking on a link directly from the HTML table.
So I've put an href="" link in the Twig file,
{% if message is defined %}
<div class="pdf">
<img height="40px" width="40px" src={{ asset('bundles/prifprotocole/images/excel.jpg') }}>
{% for entity in entities %}
<tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
<td>{% if entity.etat == 1 %} Actif {% else %} Inactif {% endif %}</td>
<td>{{ entity.titreGrade }} {{ entity.prenom }} {{ entity.nom }}</td>
<td>{{ entity.fonction }}</td>
This is how I use to export the CSV file without the link :
$response2 = $this->render('PrifProtocoleBundle:Invite:export.csv.twig', array(
'entities' => $invites));
$response2->headers->set('Content-Type', 'text/csv');
$csvfile = $response2->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
return $csvfile;
export.csv.twig file
{% for entity in entities %}
Id {{ entity.id }};
Etat {{ entity.etat }};
Titregrade {{ entity.titreGrade }};
Prenom {{ entity.prenom }};
Nom {{ entity.nom }};
Fonction {{ entity.fonction }};
{% endfor %}
Can someone give me a detailed solution on how to perform this? Much thanks!
You should simply pass the filter criterias, here the $manifestations array as a route parameter, by serializing it first.
Then, you should put a controller like downloadCsvAction() handling this route, querying the $invites from the database, and rendering your export.csv.twig template.
Another option, if you cannot serialize the data in URI, is to create a form with hidden fields containing the data. Then you replace the download link with the submit button of this hidden form.
Guillaume's answer is great if you don't mind fetching your data twice. Once when you display your page, and then when you download the CSV. Another way to do this is to cache the answer. I will give an example using memcache (which is what I use).
First, make sure that memcache is active in your php.ini.
Then before rendering your first page (after fetching the data), cache it:
$key = "csv.$userId";
$memcache = new \Memcache();
$memcache->connect($yourServerHost, $yourServerPort);
$memcache->set($key, json_encode($invites), MEMCACHE_COMPRESSED, 86400); // 24h
The trick is to find a good key, to add your report to the cache, and then be able to retrieve it. It has to be unique. Here, I assume that you have a user that is logged in, and I use the user ID to create a key. If that is not the case, you could create yourself a token, and pass it when rendering the page (along with $invites). This way, you can add the token to the URL to generate the CSV.
Then, in your second action, where you download the CSV, you just fetch the cached data:
$memcache = new \Memcache();
$memcache->connect($yourServerHost, $yourServerPort);
$json = $memcache->get($key);
$invites = json_decode($json , true);
That's it. You should probably create yourself a service for Memcache, so you don't have to create an object, and connect everytime.

Resources