Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Module {
- /**
- * @ORMManyToOne(targetEntity="Module", inversedBy="children")
- * @ORMJoinColumn(name="parent_id", referencedColumnName="ID")
- */
- protected $parent;
- /**
- * @ORMOneToMany(targetEntity="Hour", mappedBy="module", cascade={"persist","remove"}, orphanRemoval=true)
- * @ORMOrderBy({"weight"="ASC"})
- */
- protected $hours;
- /**
- * @ORMOneToMany(targetEntity="Module", mappedBy="parent", cascade={"persist","remove"}, orphanRemoval=true)
- * @ORMOrderBy({"weight"="ASC"})
- */
- protected $children;
- }
- class Hour {
- /**
- * @ORMManyToOne(targetEntity="Module", inversedBy="hours")
- * @ORMJoinColumn(name="module_id", referencedColumnName="ID")
- */
- protected $module;
- }
- class ModuleType extends AbstractType {
- public function buildForm(FormBuilderInterface $builder, array $options) {
- $builder
- ->add('hours', CollectionType::class, [
- 'entry_type' => HoursType::class,
- 'allow_add' => true,
- 'allow_delete' => true,
- 'by_reference' => false
- ])
- ->add('children', CollectionType::class, [
- 'entry_type' => OptionType::class,
- 'allow_add' => true,
- 'allow_delete' => true,
- 'by_reference' => false
- ])
- ;
- }
- public function configureOptions(OptionsResolver $resolver) {
- $resolver->setDefaults([
- 'data_class' => 'JordanCrownScopingEntityModule'
- ]);
- }
- }
- class OptionType extends AbstractType {
- public function buildForm(FormBuilderInterface $builder, array $options) {
- $builder
- ->add('hours', CollectionType::class, [
- 'entry_type' => HoursType::class,
- 'allow_add' => true,
- 'allow_delete' => true,
- 'by_reference' => false
- ])
- ;
- }
- public function configureOptions(OptionsResolver $resolver) {
- $resolver->setDefaults([
- 'data_class' => 'JordanCrownScopingEntityModule'
- ]);
- }
- }
- class HoursType extends AbstractType {
- public function configureOptions(OptionsResolver $resolver) {
- $resolver->setDefaults([
- 'data_class' => 'JordanCrownScopingEntityHour'
- ]);
- }
- }
- class ModuleController extends Controller {
- public function editAction ($id, Request $request, UserInterface $user, ModuleService $modService) {
- $em = $this->getDoctrine()->getManager();
- if( 0 === $id ) {
- $creating = true;
- $module = new Module();
- $initialOptions = new ArrayCollection();
- } elseif( $module = $em->getRepository('JCScopingBundle:Module')->findOneByIdFull($id) ) {
- $creating = false;
- $initialOptions = $module->getChildren();
- } else {
- throw $this->createNotFoundException($this->get('translator')->trans('messages.module.not_found', ['%id%' => $id]));
- }
- $form = $this->createForm(ModuleType::class, $module);
- $form->handleRequest($request);
- if( $form->isSubmitted() && $form->isValid() ) {
- if( $creating ) {
- $em->persist($module);
- }
- # !! This is my attempted workaround. !!
- foreach( $module->getChildren() as $option ) {
- if( !$initialOptions->contains($option) ) {
- $em->persist($option);
- foreach( $option->getHours() as $hourObj ) {
- $em->persist($hourObj);
- }
- }
- }
- $em->flush();
- return $this->redirectToRoute('module_list');
- }
- return $this->render('JCScopingBundle:Module:edit.html.twig', [
- 'type' => ($creating) ? 'create' : 'edit',
- 'form' => $form->createView()
- ]);
- }
- }
Add Comment
Please, Sign In to add comment