0

I have a MySQL database and frontend DataTables view and work with Symfony.

I have 2 buttons on my tables. Delete and edit. If I click on delete, it deletes the row from the DataTables and from MySqL. Works with this code:

  /**
   * @Route("/delete/{id}", name="deletepage")
   * @Template()
   */
  public function delete($id)
  {
      $em = $this->getDoctrine()->getManager();
      $events = $em -> getRepository('AppBundle:eqAli')->find($id);
      $em -> remove($events);
      $em -> flush();

      return $this->redirectToroute('homepage');
  }

I have an edit button, which redirects to an edit page, where the data from that row is auto-filled. What I am trying to achieve is that once there is made a change by a user on that page and the user clicks on save, that the new information will be updated into MySQL. So no delete, but update.

I am trying to achieve that with this code:

/**
 * @Route("/edit/update/{id}", name="updatepage")
 * @Template()
 */
public function update($id)
{
    $em = $this->getDoctrine()->getManager();
    $events = $em -> getRepository('AppBundle:eqAli')->find($id);
    $em -> persist($events);
    $em -> flush();

    return $this->redirectToroute('homepage');
}

But that does nothing. It does get the right ID, I can click, just no update into MySQL. The path etc is right because when I change persist into delete, the button works.

Anyone a solution?

3 Answers 3

1

Basically, you are fetching single eqAli entity, ordering Entity Manager to manage it and flush all changes to your entities into database.

The problem is that you are not making actual changes to your entity before saving.

Sign up to request clarification or add additional context in comments.

6 Comments

I understand what you are saying. But how can I resolve that?
You should make changes to the Entity. Now you just fetch one $events = $em -> getRepository('AppBundle:eqAli')->find($id); and persist it right away $em -> persist($events); $em -> flush(); so it is logical that there are no updates in the db. What do you want to do to the Entity??
What I want to achieve is that the changes are made into the MySQL as well after saving.
@Robbert you can, for example, do something like $events->updateWithSomeData($data); before flushing. You can get this $data from request body or your database.
But then where declare the function updateWithSomeData? And what has to be done in that function?
|
0

Ehhh ...

All should be in editAction (no redirect to update route necessary) , below you have working example, try to implement it on your case (it's a little bit more advanced , but you should catch idea ) .

```

 /**
 * @ParamConverter(name="reklama", class="SomeBundle:Reklamy")
 * @Template()
 * @param Request $request
 * @param Reklamy $reklama
 */
public function editAction(Request $request,Reklamy $reklama){
    $em = $this->getDoctrine()->getManager();
    $form=$this->createForm(new ReklamyType(),$reklama);
    if($request->getMethod()=="POST"){
        $form->handleRequest($request);
        if($form->isValid()){
            $em->flush();
            return $this->redirectToRoute('someroute')
        }
    }
    return array('form'=>$form->createView());
}

2 Comments

Thanks! What is your Request and Reklamy in your @param? Entities?
Reklamy is my entity and request is standart sf class Symfony\Component\HttpFoundation\Request;
0

You need to do something with the $events entity that you found, like so:

$events = $em -> getRepository('AppBundle:eqAli')->find($id);
...
$events->modifyDate( new DateTime() );
...
$em -> persist($events);
$em -> flush();

Where for example modifyDate is a method that is part of the eqAli Entity. I show ... before and after to indicate you might need to do other changes to your entity. In your original code, you are not changing anything at all. That's why you don't see any differences in the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.