I'll try to explain myself (my English is not as good as I wish)
- I'm very inexperienced with symfony2 and doctrine2.
- I'm trying to create a twig template and all the logic to handle modifications in, for example, user entity.
- I've made the UserType AbstractType class and can handle, modify and persist if I get just one record or, at least, if I show only one form.
- I've tried to do the same thing showing to the user every "User" in my database, and allowing him to modify and save one of them each time by clicking in submit button.
What I have right now:
src/Dummy/Form/Type/UserType.php
namespace Dummy\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setAction('')
->setMethod('POST')
->add('Name')
->add('Adress')
->add('save')
;
}
}
src/Dummy/Test/Controller/TestController.php
namespace Dummy\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Dummy\TestBundle\Entity\Users;
use Dummy\Form\Type\UserType;
class ArticuloController extends Controller
{
public function indexAction(Request $request)
{
//some stuff
$users= $this->getDoctrine()
->getRepository('DummyTestBundle:Users')
->findAll();
$forms = array();
foreach($users as $user)
{
$forms[] = $this->createForm(UserType::class, $user); //*1
}
//... Don't know how to handle request with $forms[index]->submit(...
//... Check if is valid
//... set values
//... persist
$twigForms = array();
foreach($forms as $form)
{
$twigForms[] = $form->createView()
}
//... render twig template
}
}
Also I have the entity Users which works fine, made from yaml config file as described in the documentation
What I want
Handle the request and persist the object modifications.
The part that works
Until clicking in that submit button this works fine, it shows a template with a form for each user in database, every one of them with his submit button. After pressing any of them I'm completely lost.
If I force index to be 0, it works too (but only in the first form).
Because of that, I think that I need to know the index in $users or $forms variable (marked with *1 commented point above) in order to handle that request using something like:
$forms[index]->submit($request->request->get($forms[index]->getName()));
if ($forms[index]->isValid()) {
$users[index]->setName('Name from post');
$users[index]->setAdress('Adress from post');
$em = $this->getDoctrine()->getManager();
$em->persist($users[index]);
$em->flush();
}
But don't know if this is correct or how to do it. I also read about collections, but don't know how to make this work with them.