Symfony 4 user search by email issues - php

I am trying to implement a search for user by email function into page admin's dashboard. Currently, I have hardcoded a value into $email variable just to test whether the search works. It does find the right user, but does not display anything in the twig.
Executing {{ dump() }} outputs: array:2 [▼ 0 => User {#4745 ▼ -id: 5 -
username: "test_user" -plainPassword: null -password:
"$2y$13$rGYteIrzifg9Dty.O5knOOCHQnzOtF.nZux8h1jc4sNbap5V7Xn0." -email:
"tester#test.com" } "app" => AppVariable {#2617 ▶} ]
the function I use in AdminController.php:
/**
* #Route("/admin/result", name="user_search")
* Method({"POST"})
*/
public function user_search(Request $request)
{
$email = 'tester#test.com';
$result = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
if ($result) {
return $this->render('admin/result.html.twig', $result);
}else{
return $this->render('admin/result.html.twig', [
'error' => 'No user found with this email '.$email]);
}}
result.html.twig:
{% extends 'base.html.twig' %}
{% block body %}
{% if error %}
<span class="error">{{ error }}</span>
{% endif %}
{% if result %}
<table>
<tr>
<th>Username</th><th>Email</th>
</tr>
{% for item in result %}
<tr>
<td>{{ item.getUsername }}</td><td>{{ item.getEmail }}
</td>
</tr>
{% endfor %}
</table>
{% endif %}
{{ dump() }}
{% endblock %}

In twig you assume that the result is an array. For this, uses findBy instead of findOneBy. findBy returns an array of objects with the desired search. The findOneBy return only an object with the desired search or null if results are not found.
Example:
// look for a single User by email
$result = $this->getDoctrine()
->getRepository(User::class)
->findOneBy(['email' => $email]);
// look for multiple User objects matching the email
$result = $this->getDoctrine()
->getRepository(User::class)
->findBy(['email' => $email]);

This was finally solved by these steps:
There was a typo in the hardcoded $email variable.
Changed return $this->render('admin/result.html.twig', 'result'->$result); instead of return $this->render('admin/result.html.twig', $result);
Changed <td>{{ item.username }}</td><td>{{ item.email }}</td> instead of <td>{{ result.getUsername }}</td><td>{{ result.getEmail }}</td>

Check $user with instance
if ($result instanceof User)
.......................................................................
I suggest you use defined in twig
{% if result is defined %}
{% extends 'base.html.twig' %}
{% block body %}
{% if error is defined %}
<span class="error">{{ error }}</span>
{% else %}
<table>
<tr>
<th>Username</th><th>Email</th>
</tr>
{% for item in result %}
<tr>
<td>{{ result.getUsername }}</td><td>{{ result.getEmail }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{% endblock %}

Related

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.
It's 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.
It's 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
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 it's the "{% for besoin in besoins %}" which is wrong, it should be something like "{% for besoin.dimension.stock in besoins %}" but I can't 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 didn't find how to render this array in my table so I let this solution away :/

Symfony 2 - best approach for summing results of values in related entities

My code is aimed at getting items from database. Those item may have either debit values or credit values. In template I present all of the values but would like also to provide information about sum of debit and account values connected with the account ($account_id)
What would be your best practice how to do this?
Should I simply run another two SQL queries:
first to SUM(value) where accountdebet = '.$account_id
secound to SUM(value) where accountcredit = '.$account_id?
Will it be the right approach?
Controller:
// Get items for acccount and paginate
$rp_items = $this->getDoctrine()->getManager()->getRepository('AppBundle:Items');
$query = $rp_items->createQueryBuilder('p')
->where('p.accountdebet = '.$account_id)
->orWhere('p.accountcredit = '.$account_id)
->getQuery();
$rp_paginator = $this->get('knp_paginator');
$db_pagination = $rp_paginator->paginate($query,$this->get('request')->query->getInt('page', 1),10);
// Render TPL
return $this->render('AppBundle:Accounts:items.html.twig', array('pagination' => $db_pagination, 'account' => $account));
In twig:
{% extends '::base.html.twig' %}
{% block body %}
<B>Item list for account {{ account.id }} </B><BR>
Account id: {{ account.id }}<BR>
Account marker: {{ account.marker }}<BR>
Account name: {{ account.name }}<BR>
<table class="table table-striped">
<tr>
{# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th>
<th{% if pagination.isSorted('a.itemdate') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Date', 'a.itemdate') }}</th>
<th>Document</th>
<th>{{ knp_pagination_sortable(pagination, 'Marker', 'a.marker') }}</th>
<th>Debit</th>
<th>Credit</th>
</tr>
{# table body #}
{% for item in pagination %}
<tr>
<td>{{ item.id }}</td>
<TD>{{ item.itemdate|date('Y-m-d')}}</TD>
<td>{{ item.documentid.marker }}</td>
<td>{{ item.marker }}</td>
<TD>{% if item.accountdebet.id == account.id %}
{{ item.itemvalue}}
{% endif %}
</TD>
<TD>{% if item.accountcredit.id == account.id %}
{{ item.itemvalue}}
{% endif %}</TD>
</tr>
{% endfor %}
</table>
{# display navigation #}
<div class="pagination">
{{ knp_pagination_render(pagination) }}
</div>
{% endblock %}
You can solve it in many ways but i like to make an parent entity eg ItemsCollection that holds an arrayCollection of Items. Then i give the new entity some extra functionality e.g.
public function countItemValues()
{
$total = 0;
foreach($this->items as $item)
{
$total += $item->getValue();
}
return $total;
}

The show action of Symfony 2 Sonata Admin Bundle is not working

I'm integrating Sonata Admin Bundle into my Symfony 2.6 application by following Symfony 2 jobeet tutorial. Everything is fine except the Show action. I have an entiry "Job" and so I have src/Ibw/JobeetBundle/Admin/JobAdmin.php which have a function configurShowField(ShowMapper $showMapper) like below
<?php
namespace Ibw\JobeetBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Ibw\JobeetBundle\Entity\Job;
class JobAdmin extends Admin
{
// ....
protected function configureShowField(ShowMapper $showMapper)
{
$showMapper
->add('category')
->add('type')
->add('company')
->add('webPath', 'string', array('template' => 'IbwJobeetBundle:JobAdmin:list_image.html.twig'))
->add('url')
->add('position')
->add('location')
->add('description')
->add('how_to_apply')
->add('is_public')
->add('is_activated')
->add('token')
->add('email')
->add('expires_at')
;
}
// ....
}
When I click the Show button and go to the view page (admin/ibw/jobeet/job/xxx/show), it shows none of the fields. The original template processed is /vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_show.html.twig:
{% extends base_template %}
{% block actions %}
<li>{% include 'SonataAdminBundle:Button:edit_button.html.twig' %}</li>
<li>{% include 'SonataAdminBundle:Button:history_button.html.twig' %}</li>
<li>{% include 'SonataAdminBundle:Button:list_button.html.twig' %}</li>
<li>{% include 'SonataAdminBundle:Button:create_button.html.twig' %}</li>
{% endblock %}
{% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': admin_pool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}
{% block show %}
<div class="sonata-ba-view">
***
{{ sonata_block_render_event('sonata.admin.show.top', { 'admin': admin, 'object': object }) }}
{% for name, view_group in admin.showgroups %}
<table class="table table-bordered">
{% if name %}
<thead>
{% block show_title %}
<tr class="sonata-ba-view-title">
<th colspan="2">
{{ admin.trans(name) }}
</th>
</tr>
{% endblock %}
</thead>
{% endif %}
<tbody>
{% for field_name in view_group.fields %}
{% block show_field %}
<tr class="sonata-ba-view-container">
{% if elements[field_name] is defined %}
{{ elements[field_name]|render_view_element(object) }}
{% endif %}
</tr>
{% endblock %}
{% endfor %}
</tbody>
</table>
{% endfor %}
{{ sonata_block_render_event('sonata.admin.show.bottom', { 'admin': admin, 'object': object }) }}
</div>
{% endblock %}
The inner content of <div class="sonata-ba-view"></div> is not shown except three asterisks I printed. Is there any configuration I'm missing?
You're missing a s in your function's name.
it's
configureShowFields(FormMapper $formMapper)
not
configureShowField(FormMapper $formMapper)
Try to add call in your admin.yml
ibw_jobbeet.admin.jobadmin:
class: Ibw\JobeetBundle\Admin\JobAdmin
...
calls:
- [ setTemplate, ['show', path_to_your_view.html.twig]]
Read more about templates in SonataAdmin

Symfony2 Twig Change / Replace Value

I'm new here. I'm also new in working with Symfony2.5.
I want to replace a value with another, better would be to change it via array.
It's hard to explain - here are some code parts:
Recources/Views/register.html.twig:
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{{ user.isActive }}
{% endfor %}
Here I generate the array to send to register.html.twig which looks like this:
Controller/AccountController.php
$user = $this->getDoctrine()
->getRepository('SeotoolMainBundle:User')
->findAll();
return $this->render(
'SeotoolMainBundle:Account:register.html.twig',
array('form' => $form->createView(), 'list_user' => $user)
);
It correctly outputs for isAdmin and isActive 0 or 1. Now I want to replace this output with for example isActive = 1 should Output "Active", isActive = 0 shut Output "Not active".
I hope you understand what I mean and can help me find the correct way to do this.
Thank you guys,
kind regards,
Marvin
Can't you just use an "if"?
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{% if user.isActive %}
Active
{% else %}
Inactive
{% endif %}
{% endfor %}
You can also use the ternary operator, which is more compact.
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{{ user.isActive ? 'Active' : 'Inactive' }}
{% endfor %}

Custom validator accessed twice

I have a custom validator on my Symfony2 project.
The validation works fine, but the method is somehow accessed twice.
Here is my custom validator: My other resolved question
The problem is the next:
As you can see, the error message is displayed twice. When I am trying to var dump something in the validate method, the vardump is also displayed twice. Any idea why the validate is called twice? This is called when I am using $form->bind($request); in my controller.
EDIT
Here is the twig template:
{% extends 'MerrinMainBundle::layout.html.twig' %}
{% block page_title %}
MDPI Conversion system (Merrin) 3.0 - New Conversion
{% endblock %}
{% block main %}
{% for flashMessage in app.session.flashbag.get('user-notice') %}
<div class="flash-notice">
{% autoescape false %}
{{ flashMessage }}
{% endautoescape %}
</div>
{% endfor %}
<h1>Create New Manuscript</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
</div>
{% endif %}
<form action="{{ path }}" method="POST" {{ form_enctype(form) }}>
</form>
{% endblock %}
And the controller call
public function createAction()
{
$em_scipub = $this->getDoctrine()->getManager();
$em_mdpipub = $this->getDoctrine()->getManager('mdpipub');
$enquiry = new Manuscript();
$formType = new NewManuscriptType();
$form = $this->createForm($formType, $enquiry);
$request = $this->getRequest();
$valid = true;
$error = '';
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
... do something ...
$em_scipub->persist($enquiry);
$em_scipub->flush();
$flash_message = "<a href='edit/".$enquiry->getId()."'>New Manuscript</a> sucessfully created.";
$this->get('session')->getFlashBag()->set('user-notice', $flash_message);
return $this->redirect($this->generateUrl('MerrinMainBundle_new'));
}
else
$valid = false;
}
$path = $this->generateUrl('MerrinMainBundle_new');
return $this->render('MerrinMainBundle:Pages:new_conversion.html.twig.twig', array(
'valid' => $valid,
'path' => $path,
'form' => $form->createView(),
) );
}
EDIT2:
The validate function:
public function validate($value, Constraint $constraint)
{
$doi = $value->getDoi();
preg_match('/[^\/]+/i', $doi, $publisherDoiAbbr);
if($publisherDoiAbbr[0] !== $value->getPublisher()->getDoiAbbreviation()) {
$this->context->addViolation($constraint->message_publisher_DOI);
}
else {
preg_match("/[a-z]+/",$doi, $journalDoiAbbr);
$em_mdpipub = $this->entityManager;
$journal = $em_mdpipub->getRepository('MerrinMdpiPubBundle:Journal')->findOneBy(array('doi_abbreviation' => $journalDoiAbbr));
if($journal == null) {
$this->context->addViolation($constraint->message_journal_DOI);
}
}
preg_match('/\d*$/i', $doi, $doiNumericPart);
if(strlen($doiNumericPart[0]) < 8) {
$this->context->addViolation($constraint->message_volume_issue_firstpage_DOI);
}
}
And the twig template:
{% extends 'MerrinMainBundle::layout.html.twig' %}
{% block page_title %}
MDPI Conversion system (Merrin) 3.0 - New Conversion
{% endblock %}
{% block main %}
{% for flashMessage in app.session.flashbag.get('user-notice') %}
<div class="flash-notice">
{% autoescape false %}
{{ flashMessage }}
{% endautoescape %}
</div>
{% endfor %}
<h1>Create New Manuscript</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
</div>
{% endif %}
<form action="{{ path }}" method="POST" {{ form_enctype(form) }}>
<div style="float:left;">
<table width="700">
<tr>
<td>
{{ form_label(form.doi) }}
</td>
<td>
{{ form_widget(form.doi, { 'attr': {'size': 40} }) }}
</td>
</tr>
<tr>
<td>
{{ form_label(form.publisher) }}
</td>
<td>
{{ form_widget(form.publisher) }}
</td>
</tr>
<tr>
<td>
{{ form_label(form.file) }}
</td>
<td>
{{ form_widget(form.file) }}
</td>
</tr>
<tr>
<td>
</td>
<td>
<input class="submit-confirm-button" type="submit" name="update-text" value="submit" />
<a class="cancel-link" href="{{ path('MerrinMainBundle_homepage' ) }}">Cancel</a>
</td>
</tr>
</table>
</div>
{{ form_rest(form) }}
</form>
{% endblock %}
EDIT 3:
Here is how I am applying the validator to the entity:
/**
* Manuscript
*
* #IsDOI()
* #ORM\Table(name="manuscripts")
* #ORM\Entity(repositoryClass="Merrin\MainBundle\Repository\ManuscriptRepository")
* #ORM\HasLifecycleCallbacks
*
*/
class Manuscript
{
....
}
EDIT 4:
When I try to vardump the
$form->getErrors();
I am getting an array with two values:
array(2) {
[0]=>
object(Symfony\Component\Form\FormError)#507 (4) {
["message":"Symfony\Component\Form\FormError":private]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageTemplate":protected]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageParameters":protected]=>
array(0) {
}
["messagePluralization":protected]=>
NULL
}
[1]=>
object(Symfony\Component\Form\FormError)#542 (4) {
["message":"Symfony\Component\Form\FormError":private]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageTemplate":protected]=>
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageParameters":protected]=>
array(0) {
}
["messagePluralization":protected]=>
NULL
}
}
This is possible if you are using validation groups and apply validator to several groups. And what #IsDOI() annotation mean? If its apply validation possible that you first add validator in validation.yml and second via this custom annotation.
The problem is that you're rendering it twice.
Here:
{{ form_errors(form) }}
followed by:
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
Since you are displaying the errors in single place, I recommend using only the first method.
As stated in the form_errors twig reference:
form_errors(view)
Renders any errors for the given field.
{{ form_errors(form.name) }}
{# render any "global" errors #}
{{ form_errors(form) }}
Since you have a lot of custom code (EntityManager, Validator) it is hard to tell the error with the amount of code you provide because it's not possible to recreate the error locally.
But here are my suggestions:
In your Validator there are two possible cases the following violation gets thrown
$this->context->addViolation($constraint->message_publisher_DOI);
On both occasions the error message is the same. Maybe both of these cases apply. Try to debug this by adding an individual error message for both cases.
I don't know the code of your Form-Class but maybe you apply your custom validator to multiple fields? Also remove validations for each field individually to see where the duplicate error message is thrown.
One last mistake I can imagine would be a custom Form-Template. If you have one check whether or not you maybe call the {{ form_error(form) }} block or any other block multiple times.
I hope one of my suggestions did help you.

Categories