Ill try to add some data to database and in the same controlle create user by userManager aswell. Ill made something like this, there is no error nothing when i submit this form which i rendered page just reload and this not even redirecting to route, nothing. Any idea what`s wrong ?
EDITED
My controller look right now like:
/**
* @Route("/dystrybutor/pracownicy/add", name="dystrybutor_pracownicy_add")
*/
public function new(Request $request)
{
$pracownik = new Pracownik();
$form = $this->createForm(PracownikType::class, $pracownik);
return $this->render('dystrybutor_pracownicy/new.html.twig', array(
'form' => $form->createView(),
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$id = $this->getUser()->getDystrybutorId();
$username = $form["username"]->getData();
$password = $form["password"]->getData();
$email = $form["email"]->getData();
$userManager = $container->get('fos_user.user_manager');
$user = $userManager->createUser();
$user->setUsername($username);
$user->setEmail($email);
$user->setLocked(0);
$user->setEnabled(1);
$user->setPlainPassword($password);
$user->addRole("ROLE_PRACOWNIK");
try {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
} catch (\PDOException $e) {
$this->get('session')->getFlashBag()->add(
'error', 'Wystąpił błąd przy udodawaniu użytkownika!'
);
return $this->redirect($this->generateUrl('dystrybutor_pracownicy'));
}
return $this->redirectToRoute('dystrybutor_pracownicy');
}
}
But still ORM not add user to database, nothing happend. Here what i use in this controller:
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Entity\Dystrybutor;
use App\Entity\Clients;
use App\Entity\Pracownik;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Security\Core\User\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use App\Form\PracownikType;
use FOS\UserBundle\Doctrine\UserManager;
And this is how i build my own formType, i am sure the problem is that i not submiting it properly and controller not handle it.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('name')
->add('surname')
->add('phone')
->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'Hasła muszą być takie same.',
'options' => array('attr' => array('class' => 'password-field')),
'required' => true,
'first_options' => array('label' => 'Hasło'),
'second_options' => array('label' => 'Powtórz hasło'),
))
->add('email')
->add('save', SubmitType::class, array('label' => 'Dodaj pracownika'));
}
/**
* @Route("/user/new", name="new_user")
*/
public function addUser(UserManagerInterface $userManager, EntityManagerInterface $entityManager) // use dependency injection as recommended by symfony
{
$user = $userManager->createUser();
$user->setPassword('1312adaSz');
$user->setEmail('eadas@emadi.com');
$user->setUsername('user6');
$user->setEnabled(true);
$user->setRoles(array_merge($user->getRoles(), array_diff(['ROLE_USER', 'ROLE_ADMIN'], $user->getRoles()))); // roles stored as array
$entityManager->persist($user); // add user to queue
$entityManager->flush();