I would need a little help. I'm trying to implement a search bar with symfony, this search bar in the database. What I can do but the problem is that I absolutely have to put the full name to find the person (s) corresponds to that name. Except that I would like to be able to retrieve all the people starting with the beginning of the name I entered. For example :
I type "dub" and I find: "Dubois", "Dubost", "Dubububu", ....
public function searchAction (Request $request)
{
$defaultData = array('search' => 'Type your search here');
$form = $this->createFormBuilder()
->add('search', TextType::class, array('label'=>'Surname ?','required'=>true))
->add('send', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$repository = $this
->getDoctrine()
->getManager()
->getRepository('PegasusWebBundle:Clients')
;
//$clients = $repository->findBySurname($form->get('search')->getData()); // Method 1
$clients = $repository->myFindname($form->get('search')->getData()); // Method 2 with Repository
return $this->render('PegasusWebBundle:Default:searchresult.html.twig',array(
'clients'=> $clients ));
}
else
return $this->render('PegasusWebBundle:Default:search.html.twig', array(
'form' => $form->createView(),
));
}
The repository for the method 2
`class ClientsRepository extends \Doctrine\ORM\EntityRepository
{
public function myFindname($name)
{
// Method 2
$qb = $this->createQueryBuilder('a');
$qb->where(
$qb->expr()->like('a.surname' ,':surname'))
->setParameter('surname', $name);
return $qb
->getQuery()
->getResult();
}
}`
Try adding the wildchar eg:
$query = $repo->createQueryBuilder('a')
->where('a.surname LIKE :surname')
->setParameter('surname', '%'.$name.'%')
->getQuery();
Try adding a SQL wildcard character (%) after the surname value.
Also See this as reference.
Related
I'm coding in Symfony a search bar working with a query builder for an entity named "Structure" which is related to an entity "Partenaire" (OneToMany), it's working great but the problem is that it shows all the structures and I need to display only the structures related to the Partenaire. If someone can help me to solve this issue, thank you.
PartenaireController.php:
#[Route('/{id}', name: 'app_partenaire_show', methods: ['GET', 'POST'])]
public function show(Partenaire $partenaire, EntityManagerInterface $entityManager, PartenaireRepository $partenaireRepository,Request $request, PartenairePermissionRepository $partenairePermissionRepository, StructureRepository $structureRepository): Response
{
$getEmail = $this->getUser()->getEmail();
$partenaireId = $entityManager->getRepository(Partenaire::class)->findOneBy([
'id' => $request->get('id')
]);
$search2 = $structureRepository->findOneBySomeField2(
$request->query->get('q')
);
return $this->render('partenaire/show.html.twig', [
'partenaire' => $partenaire,
'permission'=>$partenairePermissionRepository->findBy(
['partenaire' => $partenaireId],
),
'structures'=>$search2, // show all the structures
/* 'structures'=>$structureRepository->findBy( // show the structure linked to the partenaire but doesn't work with the search
['partenaire' => $partenaireId],
[],
),*/
'email'=>$getEmail,
]);
}
StructureRepository.php :
public function findOneBySomeField2(string $search2 = null): array
{
$queryBuilder = $this->createQueryBuilder('q')
->orderBy('q.id' , 'ASC');
if ($search2) {
$queryBuilder->andWhere('q.Adresse LIKE :search')
->setParameter('search', '%'.$search2.'%');
}
return $queryBuilder->getQuery()
->getResult()
;
}
as it's a One To Many relation, you should join the right table and add a where statement in your structure query like :
public function findByPartenaireWithFilter(Partenaire $partenaire, string $search2 = null): array
{
$queryBuilder = $this->createQueryBuilder('q')
->leftJoin('q.partenaire','p')
->where('p.partenaire.id = :partenaireId')
->setParameter('partenaireId', $partenaire->getId())
->orderBy('q.id' , 'ASC');
if (null !== $search2) {
$queryBuilder->andWhere('q.Adresse LIKE :search')
->setParameter('search', '%'.$search2.'%');
}
return $queryBuilder
->getQuery()
->getResult();
}
You need to limit your results to the desired Partenaire by adding a condition. You can achieve this by simply passing another argument to the repository function.
I've removed the commented code in the controller and the loading of the Partenaire entity, as it should be already loaded thanks to the magic of param converters.
#[Route('/{id}', name: 'app_partenaire_show', methods: ['GET', 'POST'])]
public function show(Partenaire $partenaire, EntityManagerInterface $entityManager, Request $request, PartenairePermissionRepository $partenairePermissionRepository, StructureRepository $structureRepository): Response
{
$getEmail = $this->getUser()->getEmail();
// The Partenaire should already be loaded
$result = $structureRepository->findByPartenaireWithFilter(
$partenaire,
$request->query->get('q')
);
return $this->render('partenaire/show.html.twig', [
'partenaire' => $partenaire,
'permission' => $partenairePermissionRepository->findBy(
['partenaire' => $partenaireId],
),
'structures' => $search2, // show all the structures
'email' => $getEmail,
]);
}
As for the repository, it's pretty straightforward: pass the loaded partenaire and add a where clause.
public function findByPartenaireWithFilter(Partenaire $partenaire, string $search2 = null): array
{
$queryBuilder = $this->createQueryBuilder('q')
->where('q.partenaire = :partenaire')
->setParameter('partenaire', $partenaire)
->orderBy('q.id' , 'ASC');
if (null !== $search2) {
$queryBuilder->andWhere('q.Adresse LIKE :search')
->setParameter('search', '%'.$search2.'%');
}
return $queryBuilder
->getQuery()
->getResult();
}
As for the permissions key in the controller, I think what you are trying to do, I'd recommend looking into security voters.
I have two entities called, Ticket & TicketUpdate.
Each Ticket can have many TicketUpdates, but every TicketUpdate can only have 1 Ticket.
Next I have a form which shows the current Ticket, but also allows me to add 1 TicketUpdate & change attributes of Ticket.
This is my controller:
//TicketController.php
...
/**
* #Route("/ticket/{id}", name="app_ticket")
*/
public function ticket(Request $request, Ticket $ticket)
{
$ticketUpdate = new TicketUpdate();
$ticketUpdate->setTicket($ticket);
$form = $this->createForm(TicketUpdateType::class, $ticketUpdate); //custom form type
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($ticketUpdate);
$em->persist($ticket);
$em->flush();
}
return $this->render('ticket/view.html.twig', ['ticket' => $ticket, 'form' => $form->createView()]);
}
...
TicketUpdateType:
//TicketUpdateType.php
...
class TicketUpdateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('text', TextareaType::class, ['label' => 'update', 'required' => false, 'attr' => ['class' => 'textarea-sm'])
->add('ticket', TicketType::class, ['label' => false, 'by_reference' => false]) //custom Type for Tickets
->add('submit', SubmitType::class, ['label' => 'save']);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => TicketUpdate::class
]);
}
}
...
However, this solution does not work for me. Symfony always wants to create a new Ticket entry, instead of changing the old one.
Is there any way to fix this?
May you know, a magic with symfony forms, with you can get an Entity (Ticket), like in your example, I dont know... but this will working:
/**
* #Route("/ticket/{ticketId}", name="app_ticket", requirements={"ticketId"="\d+"})
*/
public function ticket(Request $request, int $ticketId = 0)
{
$em = $this->getDoctrine()->getManager();
$ticket = $em->getRepository(Ticket::class)
->findOneBy([
'id' => $ticketId
]);
if ($ticket instanceof Ticket === false)
{
die('Ticket dont exist with the requested ID.'); #Just return here some error message
}
$ticketUpdate = new TicketUpdate();
//Because your setTicket() setter inside your TicketUpdate Entity
//sure have nullable typehinted argument (?Ticket $ticket)
//if this is a valid doctrine relationship
//(but if i'm wrong, please show the touched parts of your TicketUpdate entity)
$ticketUpdate->setTicket($ticket); #<-- here is an "old" Ticket $ticket
$form = $this->createForm(TicketUpdateType::class, $ticketUpdate); //custom form type
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em->persist($ticketUpdate);
//I'm working a lot with doctrine relationships
//In many time necessary using a setter in the both Entity
$ticket->addTicketUpdate($ticketUpdate); #You know this setter! Change, if my tip wrong
$em->persist($ticket);
$em->flush();
}
return $this->render('ticket/view.html.twig', [
//Now, this is an instance of the Ticket
//not an int ID!
//so if you need the ID, you can get in twig, like:
//{{ ticket.id }}
'ticket' => $ticket, #Or: $ticket->getId()
'form' => $form->createView()
]);
}
The requirements inside the #route means, the method will running only on pages, where the {ticketId} is numeric.
Update: I changed by_reference to true and removed every logic in my TicketType, which seemed to cause the issue.
Atleast for now I got it running. Here is my controller:
//TicketController.php
...
/**
* #Route("/ticket/{id}", name="app_ticket")
*/
public function ticket(Request $request, Ticket $ticket)
{
$ticketUpdate = new TicketUpdate();
$ticketUpdate->setTicket($ticket);
$form = $this->createForm(TicketUpdateType::class, $ticketUpdate); //custom form type
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($ticketUpdate);
//$em->persist($ticket); -> removed, will be automatically updated by symfony
$em->flush();
}
return $this->render('ticket/view.html.twig', ['ticket' => $ticket, 'form' => $form->createView()]);
}
...
I got a search form in page's header that must to submit to my SearchController and be handeld by searchAction in case of the user input only the query string.
This form got the ability to search in selected category and country region.
So the user can make the search in all category and all region or select category and all region or select category and region
How I can handle this? I've made the controller action that actually work when i put the data in url.
Here is my search class using QueryBuilder:
class SearchController extends Controller
{
/**
* #Route("cerca/{query}", name="search")
*/
public function searchAction(Request $request,$query)
{
$results = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->where("p.name LIKE '%$query%'")
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
return $this->render('search\search.html.twig', [
//'pagination'=>$pagination,
'query' => $query,
'region' => 'Italia',
'results' => $results,
]);
}
/**
* #Route("cerca/{region}/{query}", name="search_regioni")
*/
public function searchRegionAction($region,$query)
{
$results = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->where("p.name LIKE '%$query%'")
->andWhere("p.region Like '%$region%'")
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
$region = ucfirst($region);
return $this->render('search/search.html.twig', [
'region' => $region,
'query' => $query,
]);
}
/**
* #Route("cerca/{category}/{query}", name="search_categorie")
*/
public function searchCategoryAction($category,$query)
{
$results = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->where("p.name LIKE '%$query%'")
->andWhere("p.region Like '%$region%'")
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
$category = ucfirst($category);
return $this->render('search/search.html.twig', [
'region' => 'Italia',
'category' => $category,
'query' => $query,
]);
}
/**
* #Route("cerca/{category}/{region}/{query}", name="search_categorie_regioni")
*/
public function searchCategoryRegionAction($category,$region,$query)
{
$results = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->where("p.name LIKE '%$query%'")
->andWhere("p.region LIKE '%$region%'")
->andWhere("p.category LIKE '%$category%'")
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
$category = ucfirst($category);
$region = ucfirst($region);
return $this->render('search/search.html.twig', [
'region' => $region,
'category' => $category,
'query' => $query,
]);
}
}
How I can make the form submit to this controller? How to handle the different cases?
I think my logic is wrong too, symfony don't know if I search for a category or country region, how can I improve this?
I think instead of having different actions you should submit the data and store it in a ValueObject, e.g. a Search object:
class Search {
public $query;
public $category;
public $region;
}
You could also create a SearchType that binds a form to the value object and provides some validation and constraints, e.g. create a ChoiceLoader that loads the available categories from a database and checks that the selected one is valid. After submitting and validating the form input you retrieve the search object from the form and use it to build your Doctrine-Query. In case region is empty, e.g. not selected you just omit it from the query like this:
/** #var Search $search **/
$search = $form->getData();
$queryBuilder = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
$queryBuilder->where($queryBuilder->expr()->like('p.name', "%{$search->query}%");
if (null != $search->region) {
$queryBuilder->andWhere('region = :region')
->setParameter('region', $search->region);
}
$results = $queryBuilder->createQuery()->getResults();
Some things to note:
Do not just inject the variables into the where-clauses. This is a security problem. You should use parameters and the expression builder to safeguard against SQL injection.
You can chain clauses in the query builder (like you do above) or split them up, like I do in the if condition.
Since the form is already connected to the Search-object the fields should be pre-filled with the last inserted values, so you don't have to pass around the input separately.
I'm not 100% sure the LIKE-expression will work as expected. It can be somewhat tricky. You should search for how to use it properly.
I have a Controller with two ...Action()-methods. When I call the route /newTask with the route name newTask inside the browser I have a form to set a Task-object. After submitting via Submit-Button I want to redirect to the route /success with the route name task_success:
class FormController extends Controller {
/**
* #Route("/newTask", name="newTask")
*/
public function newAction(Request $request)
{
// create a task and give it some dummy data for this example
$task = new Task();
$task->setTask('Write a blog post');
$task->setDueDate(new \DateTime('tomorrow'));
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Create Task'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//Here I want to redirect...
return $this->redirectToRoute('task_success', array('task' => $task));
}
return $this->render('default/new.html.twig', array(
'form' => $form->createView(),
));
}
/**
* #Route("/success", name="task_success")
*/
public function successAction($task){
return $this->render('default/success.html.twig',array('task' => $task));
}
}
As you can see the method successAction has a parameter $task which I need to to show the values with the success.html.twig, but I donĀ“t need task as a part of the route (e.g. \success\{task}).
So how can I redirect with the argument $task without using it inside the route?
If you don't want display the task number in URL, you can pass its ID to the session flashBag before redirect and store it temporally:
$this->addFlash('success_task_id', $task->getId());
return $this->redirectToRoute('task_success');
Then, get this ID from session in you successAction method:
/**
* #Route("/success", name="task_success")
*/
public function successAction(){
$taskId = $this->get('session')->getFlashBag()->get('success_task_id');
$task = $this->getDoctrine()->getRepository('AppBundle:Task')->find($taskId);
return $this->render('default/success.html.twig', array('task' => $task));
}
However, if the visibility of the task number is not important, simply use it in newAction:
return $this->redirectToRoute('task_success', array('task' => $task->getId()));
This generate one URL like /success?task=1 and get the number from request query parameter bag in successAction:
public function successAction(Request $request){
$taskId = $request->query->get('task');
// ...
}
Another solution by using serialize and unserialize functions:
$this->addFlash('success_task', serialize($task));
return $this->redirectToRoute('task_success');
Then, get the serialized data from session in you successAction method:
/**
* #Route("/success", name="task_success")
*/
public function successAction(){
$task = unserialize($this->get('session')->getFlashBag()->get('success_task'));
return $this->render('default/success.html.twig', array('task' => $task));
}
I'm quite new here, be patient, please.
I'm trying to make notice board project in Symfony2 using FOSUserBundle.
I try to get logged user id to put it into form created with form builder (and then to MySQL database).
One of attempts is:
public function createNoticeAction(Request $request)
{
$notice = new Notice();
$form = $this->createFormBuilder($notice)
->add("content", "text")
->add("user_id","entity",
array("class"=>"FOS/UserBundle/FOSUserBundle:", "choice_label"=>"id"))
->add("isActive", "true")
->add("category", "entity",
array("class" => "AppBundle:Category", "choice_label" => "name"))
->add("save", "submit", array("label" => "Save"))
->getForm();
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$em->persist($notice);
$em->flush();
return $this->redirectToRoute('app_user_showuserpage');
}
I tried many solutions again and again and I get some error.
You already have the user object Symfony > 2.1.x
In you Controller like this:
$userId = $this->getUser()->getId();
...
$notice->setUserId($userId);
$em->persist($notice);
Don't ->add field in you FormBuilder, its not safely. Set this value in you Controller and don't ->add this field in FormBuilder
for symfony 3.2.13
have excelent solution (just because is working, but is dangerous if someone discover it in pure HTML)
1) first build YourFormType class.
add normal field in Forms/YourFormType.php (if not, formbuilder tell you that you passing smth not quite right (too many fields) ; -) )
$builder
->add(
'MyModelAddedById',
HiddenType::class,
[
'label' => 'echhh', //somehow it has to be here
'attr' => ['style' => 'display:none'], //somehow it has to be here
]
);
2) in your controller
public function addSomethingAction(Request $request){
$form = $this->createForm(MyModelFormType::class);
//set field value
$request->request->set("prodModelAddedById", $this->getUser()->getId());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$product = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$this->addFlash('success', 'record was added');
return $this->redirectToRoute('products');
}
return $this->render(
'default.add.form.html.twig',
[
'newprod' => $form->createView(),
]
);
}
explenation:
you are passing a field and variable to formbuilder (settig it already to default value!)
and important thing, becose of BUG in my opinion - you can't in your form type set method:
public function getBlockPrefix()
{
//return 'app_bundle_my_form_type';
}
because
$request->request->set
can't work properly if your POST data from form are in bag (parameterbag)
no entity managers, no services, no listeners...
hope it helps.