mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
infinite loop bug on flush
This commit is contained in:
parent
084d77c8f4
commit
12df38d32d
@ -20,6 +20,8 @@ use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
@ -30,6 +32,7 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Templating\EngineInterface;
|
||||
use Symfony\Component\Validator\Constraints\NotEqualTo;
|
||||
|
||||
class ReassignAccompanyingPeriodController extends AbstractController
|
||||
{
|
||||
@ -92,70 +95,75 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
||||
$total = $this->accompanyingPeriodACLAwareRepository->countByUserOpenedAccompanyingPeriod($userFrom);
|
||||
$paginator = $this->paginatorFactory->create($total);
|
||||
$periods = $this->accompanyingPeriodACLAwareRepository
|
||||
->findByUserOpenedAccompanyingPeriod(
|
||||
$userFrom,
|
||||
['openingDate' => 'ASC'],
|
||||
$paginator->getItemsPerPage(),
|
||||
$paginator->getCurrentPageFirstItemNumber()
|
||||
);
|
||||
->findByUserOpenedAccompanyingPeriod(
|
||||
$userFrom,
|
||||
['openingDate' => 'ASC'],
|
||||
$paginator->getItemsPerPage(),
|
||||
$paginator->getCurrentPageFirstItemNumber()
|
||||
);
|
||||
|
||||
// Create an array of period id's to pass into assignForm hiddenfield
|
||||
$periodIds = [];
|
||||
|
||||
foreach ($periods as $period) {
|
||||
$periodIds[] = $period->getId();
|
||||
}
|
||||
|
||||
$userFromId = null !== $userFrom ? $userFrom->getId() : null;
|
||||
|
||||
$assignForm = $this->buildReassignForm($periodIds, $userFromId);
|
||||
// Create an array of period id's to pass into assignForm hiddenfield
|
||||
$assignForm = $this->buildReassignForm($periodIds, $userFrom);
|
||||
|
||||
$assignForm->handleRequest($request);
|
||||
|
||||
$userFromId = null !== $userFrom ? $userFrom->getId() : null;
|
||||
dump($userFrom);
|
||||
if ($assignForm->isSubmitted()) {
|
||||
dump(' assign submitted');
|
||||
|
||||
$userFromId = (int) $assignForm->get('userFrom')->getData();
|
||||
$assignPeriodIds = json_decode($assignForm->get('periods')->getData(), true);
|
||||
$userTo = $assignForm->get('userTo')->getData();
|
||||
|
||||
if ($assignForm->isSubmitted()) {
|
||||
dump('reassign');
|
||||
|
||||
$userFrom = $this->userRepository->find($userFromId);
|
||||
$assignPeriodIds = json_decode($assignForm->get('periods')->getData(), true);
|
||||
$userTo = $assignForm->get('userTo')->getData();
|
||||
$userFrom = $assignForm->get('userFrom')->getData();
|
||||
|
||||
$i = 0;
|
||||
|
||||
dump($assignPeriodIds);
|
||||
foreach($assignPeriodIds as $periodId) {
|
||||
$period = $this->courseRepository->find($periodId);
|
||||
|
||||
if ($userFrom === $period->getUser()) {
|
||||
$period->setUser($userTo);
|
||||
}
|
||||
|
||||
$remainingPeriods = $this->accompanyingPeriodACLAwareRepository
|
||||
->findByUserOpenedAccompanyingPeriod(
|
||||
$userFrom,
|
||||
['openingDate' => 'ASC'],
|
||||
$paginator->getItemsPerPage(),
|
||||
$paginator->getCurrentPageFirstItemNumber()
|
||||
);
|
||||
|
||||
if ($i === 10) {
|
||||
// dd($i);
|
||||
}
|
||||
|
||||
|
||||
foreach($remainingPeriods as $period) {
|
||||
$id = $period->getId();
|
||||
|
||||
if (in_array($id, $assignPeriodIds)) {
|
||||
$period->setUser($userTo);
|
||||
$this->em->persist($period);
|
||||
dump($period);
|
||||
$i++;
|
||||
}
|
||||
|
||||
//$this->em->flush();
|
||||
|
||||
// redirect to the first page
|
||||
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return new Response(
|
||||
$this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [
|
||||
'paginator' => $paginator,
|
||||
'form' => $form->createView(),
|
||||
'assignForm' => $assignForm->createView()
|
||||
])
|
||||
);
|
||||
|
||||
} else {
|
||||
dump('no assign form');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return new Response(
|
||||
$this->engine->render('@ChillPerson/AccompanyingPeriod/reassign_list.html.twig', [
|
||||
'paginator' => $paginator,
|
||||
'periods' => $periods,
|
||||
'form' => $form->createView(),
|
||||
'assignForm' => $assignForm->createView()
|
||||
'assignForm' => isset($assignForm) ? $assignForm->createView() : null,
|
||||
])
|
||||
);
|
||||
}
|
||||
@ -170,7 +178,7 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
||||
|
||||
$builder
|
||||
->add('user', EntityType::class, [
|
||||
'class' => User::class,
|
||||
'class' => User::class, // pickUserType or PickDyamicUserType
|
||||
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
|
||||
'choice_label' => function (User $u) {
|
||||
return $this->userRender->renderString($u, []);
|
||||
@ -183,27 +191,21 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
||||
private function buildReassignForm(array $periodIds, ?int $userFromId): FormInterface
|
||||
private function buildReassignForm(array $periodIds, ?User $userFrom): FormInterface
|
||||
{
|
||||
$defaultData = [
|
||||
'userFrom' => $userFromId,
|
||||
'periods' => [],
|
||||
'userFrom' => $userFrom,
|
||||
'periods' => json_encode($periodIds),
|
||||
'assignTo' => null
|
||||
];
|
||||
|
||||
$periodsJson = json_encode($periodIds);
|
||||
|
||||
$builder = $this->formFactory->createBuilder(FormType::class, $defaultData, ['csrf_protection' => false, ]);
|
||||
$builder = $this->formFactory->createBuilder(FormType::class, $defaultData);
|
||||
|
||||
$builder
|
||||
->add('periods', HiddenType::class, [
|
||||
'data' => $periodsJson,
|
||||
])
|
||||
->add('userFrom', HiddenType::class, [
|
||||
'data' => $userFromId
|
||||
])
|
||||
->add('periods', HiddenType::class)
|
||||
->add('userFrom', HiddenType::class)
|
||||
->add('userTo', EntityType::class, [
|
||||
'class' => User::class,
|
||||
'class' => User::class, // PickUserType
|
||||
'choices' => $this->userRepository->findByActive(['username' => 'ASC']),
|
||||
'choice_label' => function (User $u) {
|
||||
return $this->userRender->renderString($u, []);
|
||||
@ -212,9 +214,28 @@ class ReassignAccompanyingPeriodController extends AbstractController
|
||||
'multiple' => false,
|
||||
'label' => 'User',
|
||||
'required' => true,
|
||||
// add a constraint: userFrom is not equal to userTo
|
||||
//'constraints' => NotEqualToh
|
||||
]);
|
||||
|
||||
// $builder->get('periods')->resetViewTransformers();
|
||||
$builder->get('userFrom')->addModelTransformer(new CallbackTransformer(
|
||||
function (?User $user) {
|
||||
if (null === $user) {
|
||||
return '';
|
||||
}
|
||||
return $user->getId();
|
||||
},
|
||||
function (?string $id) {
|
||||
if (null === $id) {
|
||||
return null;
|
||||
}
|
||||
if (!is_int((int) $id)) {
|
||||
throw new TransformationFailedException("the user id is not a numeric");
|
||||
}
|
||||
|
||||
return $this->userRepository->find((int) $id);
|
||||
}
|
||||
));
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user