search index is not deleted when entity value is deleted - php

I am working on a job portal's site built in Symfony 2.1. I have implemented ewz/EWZSearchBundle for job search process which is as given below:
/**
* Displays a form to create a new Job entity.
*
* #Route("/new", name="job_new")
* #Template("JobBundle:Job:new.html.twig")
*/
public function newAction(Request $request)
{
$entity = new Job();
$form = $this->createForm(new JobType(), $entity);
if('POST'===$request->getMethod()){
$form->bind($request);
if ($form->isValid()) {
$entity->setOwner($this->getEmployer());
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$location = $entity->getLocation();
$loName = array();
foreach ($location as $key => $loc) {
$loName[] = $loc->getName();
}
$industry = $entity->getIndustry();
$inName = array();
foreach ($industry as $key => $ind) {
$inName[] = $ind->getTitle();
}
$skills = $entity->getSkills();
$skillName = array();
foreach ($skills as $key => $skl) {
$skillName[] = $skl->getTitle();
}
$education = $entity->getEducation();
$educationName = array();
foreach ($education as $key => $edu) {
$educationName[] = $edu->getTitle();
}
$search = $this->get('ewz_search.lucene');
$document = new Document();
$document->addField(Field::keyword('key', $entity->getId()));
$document->addField(Field::text('title', $entity->getTitle()));
$document->addField(Field::text('url', $this->generateUrl('job_show', array('slug' => $entity->getSlug()))));
$document->addField(Field::unstored('body', $entity->getDescription()));
$document->addField(Field::text('location', implode(" ", $loName)));
$document->addField(Field::text('industry', implode(" ", $inName)));
$document->addField(Field::text('skills', implode(" ", $skillName)));
$document->addField(Field::text('education', implode(" ", $educationName)));
$search->addDocument($document);
$search->updateIndex();
return $this->redirect($this->generateUrl('job_show', array('slug' => $entity->getSlug())));
}
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
It is working very fine when a candidate is searching jobs by any of following fields: location, industry, skills or education. But there are two issues of mine:
In this process for every field (locations, skills, industry or education) only one job search is applicable while I want separate search forms, for example in location-wise search, only location field is matched not the other remaining fields.
The second problem is when an employer deletes his job, the indexes of deleted jobs are also shown in the search process while after the job was deleted, the index of the deleted job should also be deleted from search index.
I have tried to delete the search index on the time of job_delete action by following code:
/**
* Deletes a Job entity
* #Route("/{slug}/delete", name="job_delete")
* #Method("POST")
*/
public function deleteAction(Request $request, $slug)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('JobBundle:Job')
->findOneBy(array('slug' => $slug));
if (!$entity) {
throw $this->createNotFoundException('Unable to find Job entity.');
}
$form = $this->createDeleteForm($entity->getSlug());
$form->bind($request);
if ($form->isValid()) {
$this->checkOwnerSecurity($entity);
$em->remove($entity);
$em->flush();
$search = $this->get('ewz_search.lucene');
$index = $search->getIndex();
$removePath = 'http://localhost/Jobtook/web/app_dev.php/employer/job/{slug}/delete';
$hits = $index->find('job_delete' . $removePath);
foreach ($hits as $hit) {
$index->delete($hit->id);
}
}
return $this->redirect($this->generateUrl('job'));
}
private function createDeleteForm($slug)
{
return $this->createFormBuilder(array('slug' => $slug))
->add('slug', 'hidden')
->getForm()
;
}
But by this code when I create many jobs and delete only one job, then all jobs are deleted from the search index even remaining jobs, that still exist in database. I don't know what I am doing wrong.

Related

Multiple forms for entity instances - symfony3

I'm trying to create a form that would allow a manager to approve a list of time off requests (also planning to have a todo list and want to be able to mark them as done).
I have read [Generate same form type on same page multiple times Symfony2 (as well as several others) and I am close to understanding but I'm fairly to new to Symfony and not clear on what parts of the code should go in what files. I am using a form type and a controller in Symfony3 with Doctrine.
I have list of the entity instances that were returned from a query ($em->createQuery) in the controller and I am looking to produce a form for each entity instance or even two forms per entity (one for approve and one for reject).
The referenced question says you need a loop to display and save them. My intention is to only work on (submit) one at a time. I assume this part of the code would go in the controller?
I am using an indexAction for the controller but using it more like an Edit action since I will be processing forms, so I pass in a Request object and the objects as parameters.
>
class HRMgrController extends Controller
{
/**
* Lists all manager role requests and provide a means to approve/deny.
*
* #Route("/", name="hrmgr_index")
* #Method({"GET", "POST"})
* #Security("has_role('ROLE_APP_MANAGER')")
*/
public function indexAction(Request $request, TimeOffRequest $timeOffRequest)
{
if (!empty($timeOffRequest)) {
$form = $this->createForm('CockpitBundle\Form\TORApprovalType', $timeOffRequest);
print "TOR Id = " . $timeOffRequest->getId() . "<BR>";
$em = $this->getDoctrine()->getManager();
$form->handleRequest($request);
print "Form name = " . $form->getName() . "<BR>";
if ($form->isSubmitted() && $form->isValid()) {
if ($form->get('approve')->isClicked()) {
print "This puppy was approved";
$timeOffRequest['status'] = 4;
}
if ($form->get('reject')->isClicked()) {
print "This puppy was rejected";
$timeOffRequest['status'] = 1;
}
$this->getDoctrine()->getManager()->flush();
print "At least its there<BR>";
// return $this->redirectToRoute('hrmgr_index');
} else {
print "did not detect form submission<BR>";
}
}
$emp = new \CockpitBundle\Entity\Employee();
$date = new \DateTime();
$year = $date->format('Y');
$username = $this->getUser()->getUserName();
$user = $em->getRepository('CockpitBundle:Employee')->findByUsername($username);
$employees = $em->getRepository('CockpitBundle:Employee')->htmlContact($user);
$tors = $em->getRepository('CockpitBundle:TimeOffRequest')->findMgrUnapprovedTORs($user->getId());
$timeoff = "<h3>Unapproved Time Off Requests</h3>";
$actions = true;
$torforms = [];
foreach ($tors as $tor) {
$target = $this->generateUrl('hrmgr_index',array("tor_id" => $tor->getId()));
$torforms[] = $this->actionForm($tor,$target)->createView();
}
return $this->render('hrmgr/index.html.twig', array(
'torforms' => $torforms,
));
I have the forms working nowbut when I submit them the isSubmitted() doesn't seem to be working. It outputs the "did not detect form submission" currently.
So when I have multiple forms and I submit one, does the handleRequest get the right one? I think I might be confusing two concepts here as well. I recently changed the code to submit the ID of the timeOffRequest as a parameter to the route. It is properly picking that up which allows me to potentially update the form but that part of the code doesn't seem to be working.
I noticed that if I look at the debugger, I get something like:
> approval_form_2
[▼
"reject" => ""
"_token" => "IE1rGa5c0vaJYk74_ncxgFsoDU7wWlkAAWWjLe3Jr1w"
]
if I click the reject button. I get a similar form with "approve" if I click the approve button so it seems like I am close. Also, the proper ID shows up from the route given in the action.
Here is the form generator:
<?php
namespace CockpitBundle\Form;
use CockpitBundle\Entity\Employee;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class TORApprovalType extends AbstractType
{
private $nameSuffix = null;
private $name = 'time_req_approval';
public function __constructor(string $suffix = null) {
//parent::__construct();
$this->nameSuffix = $this->generateNameSuffix();
}
private function generateNameSuffix() {
if ($this->nameSuffix == null || $this->nameSuffix == '') {
$generator = new SecureRandom();
//change data to alphanumeric string
return bin2hex($generator->nextBytes(10));
}
return $this->nameSuffix;
}
public function setNameSuffix($suffix){
$this->nameSuffix = $suffix;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Build your form...
$builder->add('approve', SubmitType::class, array(
'label' => "Approve",
'attr' => array("class"=>"action-approve"),
));
$builder->add('reject', SubmitType::class, array(
'label' => "Reject",
'attr' => array("class"=>"action-reject"),
));
//$builder->add('employee');
}
public function getName() {
if ($this->nameSuffix == null || $this->nameSuffix == "" ) {
$this->nameSuffix = $this->generateNameSuffix();
}
return $this->name .'_'. $this->nameSuffix;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CockpitBundle\Entity\TimeOffRequest'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'cockpitbundle_timeoffrequest';
}
}
Any clues? (sorry I am on vacation so not particular quick with updates.
You can do multiple submit button: check your formtype
->add('approve', 'submit')
->add('reject', 'submit')
then in your controller
if ($form->isValid()) {
// ... do something
// the save_and_add button was clicked
if ($form->get('approve')->isClicked()) {
// probably redirect to the add page again
}
if ($form->get('reject')->isClicked()) {
// probably redirect to the add page again
}
// redirect to the show page for the just submitted item
}
I was able to get it working with the following builder.
$builder->add('approve', SubmitType::class, array(
'label' => "Approve",
'attr' => array("class"=>"action-approve"),
));
$builder->add('reject', SubmitType::class, array(
'label' => "Reject",
'attr' => array("class"=>"action-reject"),
));
Then in the controller form I generate and process the forms as such. Not sure if it is the optimal way but it works find. Of course, this approach redraws the whole list each time, but that is fine for what I'm doing.
class HRMgrController extends Controller
{
/**
* Lists all manager role requests and provide a means to approve/deny.
*
* #Route("/", name="manager_home")
* #Method({"GET"})
* #Security("has_role('ROLE_APP_MANAGER')")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$emp = new \CockpitBundle\Entity\Employee();
$employeeSummary = [];
$date = new \DateTime();
$year = $date->format('Y');
$username = $this->getUser()->getUserName();
$user = $em->getRepository('CockpitBundle:Employee')->findByUsername($username);
$myemployees = $em->getRepository('CockpitBundle:Employee')->findManagersEmployees($user);
$torRep = $em->getRepository('CockpitBundle:TimeOffRequest');
$toas = [];
$torforms = [];
foreach ($myemployees as $employee) {
$tors = $torRep->findAllMine($employee,$year);
$toas[$employee->getDisplayName()] = $em->getRepository('CockpitBundle:TimeOffAllocation')->getEmpAllocation($employee->getId(),$year);
$employeeSummary[$employee->getDisplayName()] = $torRep->mySummary($tors,$toas[$employee->getDisplayName()]);
if (array_key_exists('items',$employeeSummary[$employee->getDisplayName()]['Vacation']['Requested'])) {
foreach ($employeeSummary[$employee->getDisplayName()]['Vacation']['Requested']['items'] as $tor) {
$target = $this->generateUrl('hrmgr_tor_approval',array("tor_id" => $tor->getId()));
$torforms[] = $this->actionForm($tor,$target)->createView();
}
}
if (array_key_exists('items',$employeeSummary[$employee->getDisplayName()]['Sick Time']['Requested'])) {
foreach ($employeeSummary[$employee->getDisplayName()]['Sick Time']['Requested']['items'] as $tor) {
$target = $this->generateUrl('hrmgr_tor_approval',array("tor_id" => $tor->getId()));
$torforms[] = $this->actionForm($tor,$target)->createView();
}
}
if (array_key_exists('Time Off',$employeeSummary[$employee->getDisplayName()]) &&
array_key_exists('items',$employeeSummary[$employee->getDisplayName()]['Time Off']['Requested'])) {
foreach ($employeeSummary[$employee->getDisplayName()]['Time Off']['Requested']['items'] as $tor) {
$target = $this->generateUrl('hrmgr_tor_approval',array("tor_id" => $tor->getId()));
$torforms[] = $this->actionForm($tor,$target)->createView();
}
}
}
return $this->render('hrmgr/index.html.twig', array(
'employeeSummary' => $employeeSummary,
'torforms' => $torforms,
'year' => $year,
));
}
/**
* Lists all manager role requests and provide a means to approve/deny.
*
* #Route("/{tor_id}", name="hrmgr_tor_approval")
* #Method({ "POST" })
* #ParamConverter("timeOffRequest", class="CockpitBundle:TimeOffRequest", options={"id"="tor_id"})
* #Security("has_role('ROLE_APP_MANAGER')")
*/
public function approvalAction(Request $request, TimeOffRequest $timeOffRequest)
{
if (!empty($timeOffRequest)) {
$form = $this->createForm('CockpitBundle\Form\TORApprovalType', $timeOffRequest);
$id = $timeOffRequest->getId();
$em = $this->getDoctrine()->getManager();
$form->handleRequest($request);
$postparams = $request->request->all();
if (array_key_exists("approval_form_$id",$postparams)) {
// Form was submitted
if (array_key_exists("approve",$postparams["approval_form_$id"])) {
$status = $em->getReference('CockpitBundle\Entity\TimeOffStatus', 4);
$timeOffRequest->setStatus($status);
$timeOffRequest->setApprovedDate(new \DateTime);
$em->persist($timeOffRequest);
$em->flush($timeOffRequest);
}
if (array_key_exists("reject",$postparams["approval_form_$id"])) {
$status = $em->getReference('CockpitBundle\Entity\TimeOffStatus', 1);
$timeOffRequest->setStatus($status);
$timeOffRequest->setApprovedDate(new \DateTime);
$em->persist($timeOffRequest);
$em->flush($timeOffRequest);
}
} else {
print "Form did not exist<BR>";
}
return $this->redirectToRoute('manager_home');
}
}
public function actionForm($tor,$target) {
return $this->get('form.factory')->createNamedBuilder('approval_form_'.$tor->getId(), \CockpitBundle\Form\TORApprovalType::class, $tor,
array("action"=> $target))->getForm();
}
}

Zend Framework: How to save Objects from a select form to a database?

I started to work with Zend Framework 1.12.7 for a student project. I'm porgramming a User Class with an addAction and a form with various text fields and two dropdown menus that I want to fill with objects from the database (groups and roles). The idea is to select one object from the dropdown list (represented by its name) and save the id of the appropriate object to the database.
I already found out, that Zend Framework is working with associative arrays and I'm already able to select the names out of the dropdown menu. But however, there is still nothing saved to the database.
I provide you my controller, form and model code for the role object:
Controller:
public function addAction()
{
$request = $this->getRequest();
$rollen = new Application_Model_Rolle();
$mapper = new Application_Model_RolleMapper();
$rollen = $mapper->fetchAll();
$options;
foreach ($rollen as $rolle) {
$key = (string) $rolle->bezeichnung;
$options[] = array($key => $rolle->bezeichnung );
$values[] = array($key => $rolle);
}
$form = new Application_Form_Benutzer();
$form->getElement('rolle')->setMultiOptions($options);
$form->getElement('rolle')->setValues($values);
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
$benutzerdaten = new Application_Model_Benutzer($form->getValues());
die($form->getValue('rolle'));
$mapper = new Application_Model_BenutzerMapper();
$mapper->save($benutzerdaten);
return $this->_helper->redirector('index');
}
}
Form:
$this->addElement('select', 'rolle', array(
'label' => 'Rolle:',
'required' => false,
));
Model:
public function fetchAll()
{
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Benutzer();
$entry->setb_Id($row->b_id)
->setNachname($row->nachname)
->setVorname($row->vorname)
->setBenutzername($row->benutzername)
->setEmail($row->email)
->setTelefon($row->telefon)
->setPasswort($row->passwort);
$gruppeID = $row->g_id;
$gruppe = new Application_Model_Gruppe();
$mapper = new Application_Model_GruppeMapper();
$gruppe = $mapper->find($gruppeID, $gruppe);
$rolleID = $row->r_id;
$rolle = new Application_Model_Rolle();
$mapper = new Application_Model_RolleMapper();
$rolle = $mapper->find($rolleID, $rolle);
$entry->setRolle($rolle);
$entry->setGruppe($gruppe);
$entries[] = $entry;
}
return $entries;
}
So how can I access the chose object and save its ID to the database?
Thank you very much for your help!
Cheers!
Martin

Symfony 2 - Entity has to be managed or scheduled for removal for single computation

When I am submitting symfony2 form I got the following error:
Entity has to be managed or scheduled for removal for single computation
What does this error mean?
I am using the form which is aimed at adding new item to DB. I have multiple ManyToOne relations in the form.
/**
* This code is aimed at checking if the book is choseen and therefore whether any further works may be carried out
*/
$session = new Session();
if(!$session->get("App_Books_Chosen_Lp")) return new RedirectResponse($this->generateUrl('app_listbooks'));
$request = $this->get('request');
$em = $this->getDoctrine()->getManager();
if($item_id != null)
{
/* THIS CODE IS NOT EXECUTED IN THE GIVEN CASE */
}
else
{
// Add
$item = new Items();
$form = $this->createForm(new ItemsType(), $item);
$form->add('save', 'submit', array('label' => 'Add item'));
}
$form->remove('documentid');
$form->remove('book');
$form->handleRequest($request);
if ($form->isValid()) {
if($item_id != null)
{
/* THIS CODE IS NOT EXECUTED IN THE GIVEN CASE */
}
else
{
/* HERE ERROR OCCURS */
// Add
$book = $em->getReference('AppBundle:Books', $session->get("App_Books_Chosen_Lp"));
if( $book ) $item->setBook($book);
$doc = $em->getReference('AppBundle:Documents', $doc_id);
if( $doc ) $item->setDocumentid($doc);
$em->flush($item);
$session = new session();
$session->getFlashBag()->add('msg', 'Item was added.');
$url = $this->generateUrl("app_documents_details", array("id" => $doc_id));
return $this->redirect($url);
}
You need to persist your entity to let the EntityManager knows that it exists.
$em->persist($item);
$em->flush($item);

Print all Symfony2 forms

I currently have a script in Symfony where I generate a form in a controller. This form shows the content of the Entity "Page". If the user edits the form, and submits it, the form adjusts the corresponding data in the database.
/**
* Webpage allowing user to edit a page and her attributes
*
* #Route("/edit")
*/
public function editAction(Request $request)
{
/*
* Get an array of all the current pages stored in the database.
*
* foreach loop through each website and create a seperate form for them
*
*/
$em = $this->getDoctrine()->getManager();
$pages = $em->getRepository(Page::class)->findAll();
foreach ($pages as $page) {
$editform= $this->createFormBuilder($page)
->add('name', 'text')
->add('location', 'url')
->add('displayTime', 'integer')
->add('save', 'submit', array(
'label' => 'Edit page'
))
->getForm();
$editform->handleRequest($request);
if ($editform->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
return new Response('Page edited successfully - immediately effective');
}
}
return $this->render('WalldisplayBundle:Walldisplay:edit.html.twig',
array(
'editform' => $editform->createView()
));
}
Unfortunately, this only prints a form with the last entry in the database. What I'd like is to have a form created for -every- entry in the database, not just the last one. I've tried iterating through the Doctrine repository, no luck however. How could I solve this problem?
Maybe it will work. I have not tested.
/**
* Webpage allowing user to edit a page and her attributes
*
* #Route("/edit")
*/
public function editAction(Request $request)
{
/*
* Get an array of all the current pages stored in the database.
*
* foreach loop through each website and create a seperate form for them
*
*/
$em = $this->getDoctrine()->getManager();
$pages = $em->getRepository(Page::class)->findAll();
foreach ($pages as $key=>$page) {
$editforms[$key] = $this->createFormBuilder($page)
->add('name', 'text')
->add('location', 'url')
->add('displayTime', 'integer')
->add('save', 'submit', array(
'label' => 'Edit page'
))
->getForm();
foreach($editforms as $editform){
$editform->handleRequest($request);
if ($editform->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
}
}
return new Response('Page edited successfully - immediately effective');
}
foreach($editforms as $editform){
$editform->handleRequest($request);
if ($editform->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
}
foreach($editforms as $editform){
$arrayForm[$editform] = $editform->createView();
}
return $this->render('WalldisplayBundle:Walldisplay:edit.html.twig', $arrayForm);
}

Symfony2, How do I perform a database retrieve in Doctrine with a drop down list form?

I have a form that will perform a search action with a drop down list of choices of category to search. The drop down list are subjects to search by. So:
Search by:
1) Invoice #
2) Tracking #
3) BL #
Then enter in the value and submit to search.
I have my form:
// src Bundle\Form\Type\SearchType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('kind', 'choice', array(
'choices' => array(
'invoice' => 'Invoice #',
'trackingno' => 'Tracking Number'
'blno' => 'BL #',
),
'label' => 'Search by: '
))
->add('value', 'text', array(
'label' => false
))
->add('Submit','submit');
}
With this function in the controller:
public function getForm() {
$form = $this->createForm(new SearchType(), array(
'action' => $this->generateUrl('search_view'),
'method' => 'POST'
) );
return $form;
}
With the action going to 'search_view' function:
/**
* #param Request $request
* #Route("/results/", name="search_view")
*/
public function searchAction(Request $request) {
$kind = $request->get('kind');
$value = $request->get('value');
$em = $this->getDoctrine()->getManager();
$findCfs = $em->getRepository("CFSBundle:Cfs")
->searchCfs($kind, $value);
return $this->render("CFSBundle:Search:index.html.twig", array(
'results' => $findCfs
));
}
My problem is I do not where to go to perform the database retrieval base on the category. This is what I have in the repository:
public function searchCfs($kind, $value) {
$query = $this->getEntityManager()
->createQuery('
SELECT
c.blno, m.ctrno, c.carrier, m.refno
FROM
CFSBundle:Cfs c
LEFT JOIN
cfs.refno m
WHERE
:kind LIKE :value
')->setParameter("kind", $kind)
->setParameter("value", $value);
try {
return $query->getResult();
} catch(\Doctrine\ORM\NoResultException $e) {
return null;
}
}
Of course this isn't working. I thought about creating different queries for each category and have the conditions submit according to its category, but I was wondering if there was one simple solution to this?
I'd advise using the QueryBuilder class when writing a query such as this, rather than writing DQL directly.
You could then do something like the following in your repository:
const KIND_INVOICE_NO = 'invoice';
const KIND_TRACKING_NO = 'tracking';
const KIND_BL_NO = 'blno';
public function searchCfs($kind, $value) {
$queryBuilder = $this->createQueryBuilder('c')
->select('c.blno, m.ctrno, c.carrier, m.refno')
->leftJoin('cfs.refno', 'm');
if ($kind === self::KIND_INVOICE_NO) {
$queryBuilder->where('c.invoiceno = :queryValue');
} elseif ($kind === self::KIND_TRACKING_NO) {
$queryBuilder->where('m.ctrno = :queryValue')
} elseif ($kind === self::KIND_BL_NO) {
$queryBuilder->where('c.blno = :queryValue')
}
$queryBuilder->setParameter('queryValue', $value);
$query = $queryBuilder->getQuery();
try {
return $query->getResult();
} catch(\Doctrine\ORM\NoResultException $e) {
return null;
}
}

Categories