0

I want update row setting new values from array, now Iset one by one aribute:

    $em = $this->getDoctrine()->getManager();
    $company = $em->getRepository('CatalogWebBundle:ComCompany')
        ->findOneBy(
            array('cmpCode' => $id)
        );

    $company->setCmpName($_POST['name']);
    $company->setCmpCode($_POST['code']);
    $em->flush();

Maybe exist solution to set all atributes from array, some like this ?

$company = $_POST; 
2
  • Generally you have two options, you can use symfony forms or you can add a function to your entity that populates values from an array. Commented Dec 20, 2014 at 7:33
  • 1
    Why use a framework if you're not going to utilize the tools that they offer? Commented Dec 20, 2014 at 15:10

1 Answer 1

2

Consider using Symfony Form and use like this:

<?php

$request = $this->getRequest(); // or inject Request to the action method like myAction(Request $request)

$em = $this->getDoctrine()->getManager();
$company = $em->getRepository('CatalogWebBundle:ComCompany')
  ->findOneBy(
      array('cmpCode' => $id)
);

$form = $this->createForm('formName', $company);
$form->handleRequest($request);

if($form->isValid()) {
  // if you want, you can get entity here,
  // but passing an entity in 2nd param of createForm
  // provides automatic binding data
  $data = $form->getData();

  $em->flush();

  // do some action after submitting form...
  return $this->redirect($this->generateUrl('companies'));
}

Read more about creating forms: http://symfony.com/doc/current/book/forms.html

And about regitering forms as services for futher use named form in createForm('NAME_HERE', $bindObjectHere): http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services

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

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.