Noobtastic
Noobtastic

Reputation: 187

Symfony doctrine, update entity without form?

Not sure if I have this right but I made my own custom form in the twig template with the action path going to a controller that will update the entity. I've only ever seen update methods using form with $form->handleRequest($request), then with a $em->flush(); Since I haven't made a form through Symfony's form component, I don't know how I can access it from the template in order to flush it into the database.

Here's how I have my action controller:

/**
 * @param $subid
 * @param Request $request
 * @Route("/editparts/{subid}/", name="updateparts")
 * @Template("editparts.html.twig")
 * @Method("POST")
 */
public function updatePartsAction(Request $request, $subid) {
    $r = $this->getDoctrine()->getManager();

    $entity = $r->getRepository('MainBundle:MainSub')->findOneById($subid);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Parts entity to edit.');
    }

    // what is this step???

    $r->flush();

.....

My form in the twig template is like this:

{% if parts is defined %}
     <div class="inventorysearch">
       <form action="{{ path('updateparts', {'subid' : parts.subid}) }}" method="POST" >
            <input type="text" name="part" value="{{ parts.part }}" disabled><br />
            <input type="text" name="batch" value="{{ parts.batch }}" disabled><br />
            <input type="text" name="rack" required="required" value="{{ parts.rack }}"><br />
           <input type="text" name="acode" value="{{ parts.acode }}"><br />
           <input type="text" name="bcode" value="{{ parts.bcode }}"><br />
           <input type="integer" name="qty" required="required" value="{{ parts.qty }}"><br />
            <button type="submit" name="submit">Update</button>
       </form>
     </div>
    {% endif %}

Upvotes: 2

Views: 6961

Answers (2)

skrilled
skrilled

Reputation: 5371

That concept goes against the entire purpose of MVC (trying to do it in the template/view), so I'm not sure what you meant by your last statement.

However, in your controller you should access the entity (model) via the entities methods.

I.e. if I have a $user entity with a username property, I should have a setUsername method and can do:

 $user->setUsername('theusername');
 $em->persist($user); 
 $em->flush();

Note that I used $em instead of your $r out of habit (it's what the documentation shows and what most programmers will use).

Also, using the form component is still a better method in the long haul.

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64496

The best and recommended way is to use symfony's form builder

public function updatePartsAction(Request $request, $subid) {
    $r = $this->getDoctrine()->getManager();

    $entity = $r->getRepository('MainBundle:MainSub')->findOneById($subid);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Parts entity to edit.');
    }
    $form= $this->createFormBuilder($entity)
           ->add('part')
           ->add('batch')
           .... and so on the properties from your entity that you want them to edit
           ->getForm();
 if ($this->getRequest()->getMethod() == "POST") {
        $form->handleRequest($request)
        if ($form->isValid()) {
        $r->persist($form->getData());
        $r->flush();
        }
  }

}

In twig just render your {{ form(form) }}

The other way you ask for is not recommended and not a good practice but it depends on you how you are going to code your app the good way or bad way

public function updatePartsAction(Request $request, $subid) {
    $r = $this->getDoctrine()->getManager();

    $entity = $r->getRepository('MainBundle:MainSub')->findOneById($subid);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Parts entity to edit.');
    }
  if ($this->getRequest()->getMethod() == "POST") {
    $entity->setPath('get values from request')
     and others setters to which you want them to edit        
        $r->persist($entity);
        $r->flush();

  }
}

Upvotes: 2

Related Questions