Bonjour à tous !
Alors voilà, je suis en train de faire un calendrier afin que mes utilisateurs puissent créer des évènements dessus. Je me heurte à une petite chose, c'est que je n'arrive pas à faire correctement mes évenements.
Dans un premier temps voilà mon controller :
Mon Calendar :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public function calendarAction() { if(!$this->get('security.context')->isGranted('ROLE_USER')) { throw new AccessDeniedHttpException('Accès limité aux personnes inscritent au site'); } else { $calendar = new Calendar(); $userManager = $this->get('fos_user.user_manager'); $userCurrent = $this->container->get('security.context')->getToken()->getUser(); $user = $userManager->findUserBy(array('username' => $userCurrent)); if($user->getCalendar() != null) { $calendar = $user->getCalendar(); } $event = $this->InitializeEvent($calendar); $calendar->addEvent($event); $userManager->updateUser($user); $form = $this->createForm(new CalendarType(), $calendar); $formHandler = new CalendarHandler($form, $this->get('request'), $userManager, $user); if($formHandler->process()) { return $this->redirect($this->generateUrl('mon_site_user_calendar')); } return $this->render('monsiteCalendarBundle:monBoard:calendar.html.twig', array('form' => $form->createView())); } } private function InitializeEvent(Calendar $calendar) { $event = new Event(); $event->setCalendar($calendar); return $event; }
Mes evenements :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 <?php namespace monsite\CalendarBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Rizza\CalendarBundle\Entity\Calendar as BaseCalendar; /** * monsite\CalendarBundle\Entity\Calendar * * @ORM\Table(name="calendar") * @ORM\Entity */ class Calendar { /** * @var integer $id * * @ORM\Id * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var Event $event * * @ORM\OneToMany(targetEntity="monsite\CalendarBundle\Entity\Event", cascade={"persist"}, mappedBy="calendar") */ private $events; private $dateClicked; public function __construct() { $this->events = new \Doctrine\Common\Collections\ArrayCollection(); } public function addEvent(Event $events) { $this->events[] = $events; $events->setCalendar($this); return $this; } public function removeEvent(Event $events) { $this->events->removeElement($events); $events->setCalendar(null); } public function getEvents() { return $this->events; } public function getId() { return $this->id; } public function getDateClicked() { return $this->dateClicked; } public function setDateClicked($dateClicked) { $this->dateClicked = $dateClicked; } }
Mon type evenement :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 <?php namespace monsite\CalendarBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * monsite\CalendarBundle\Entity\Event * * @ORM\Table(name="event") * @ORM\Entity */ class Event { /** * @var integer $id * * @ORM\Id * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var date $datebegin * * @ORM\Column(name="date_begin", type="datetime", nullable=true) */ private $datebegin; /** * @var date $dateend * * @ORM\Column(name="date_end", type="datetime", nullable=true) */ private $dateend; /** * @var date $dateend * * @ORM\ManyToOne(targetEntity="monsite\CalendarBundle\Entity\Status", cascade={"persist"}) */ private $status; /** * @ORM\ManyToOne(targetEntity="monsite\CalendarBundle\Entity\Calendar", inversedBy="events") * @ORM\JoinColumn(nullable=false) */ private $calendar; public function __construct() { $this->datebegin = new \DateTime(); $this->dateend = new \DateTime(); } public function getDateBegin() { return $this->datebegin; } public function setDateBegin(\DateTime $datebegin) { return $this->datebegin = $datebegin; } public function getDateEnd() { return $this->dateend; } public function setDateEnd(\DateTime $dateend) { return $this->dateend = $dateend; } public function getStatus() { return $this->status; } public function setStatus(Status $status) { return $this->status = $status; } public function getCalendar() { return $this->calendar; } /** * Set Calendar * * @param Calendar $calendar */ public function setCalendar(Calendar $calendar) { $this->calendar = $calendar; } public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 <?php namespace monsite\CalendarBundle\Form\Type; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\AbstractType; use monsite\CalendarBundle\Entity\Event; use Doctrine\ORM\EntityManager ; use Doctrine\ORM\EntityRepository; class EventType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('datebegin', 'date', array( 'input' => 'datetime', 'widget' => 'choice', 'years' => range(date('Y'), date('Y') + 5))) ->add('dateend', 'date', array( 'input' => 'datetime', 'widget' => 'choice', 'years' => range(date('Y'), date('Y') + 5))) ->add('status', new StatusType()) ->add('id', 'hidden'); } public function getName() { return 'mon_site_event_type'; } public function getDefaultOptions(array $options) { return array( 'data_class' => 'monsite\CalendarBundle\Entity\Event', ); } }
Et le problème doit venir de ma vue :
Ma vue (partielle) :
Voilà mon raisonnement :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 <form action="{{ path('mon_site_user_calendar') }}" {{ form_enctype(form) }} method="POST" > {% set event_ok = form.events[0] %} {% for event in form.events %} {% if event_ok.id < event.id %} {% set event_ok = event %} {% endif %} {% endfor %} {{ form_errors(event_ok.datebegin) }} {{ form_widget(event_ok.datebegin) }} {{ form_errors(event_ok.dateend) }} {{ form_widget(event_ok.dateend) }} {{ form_errors(event_ok.status) }} {{ form_widget(event_ok.status) }} <input type="submit" value="Save"> </form>
Je créer un évènement, je le mets en base. Je récupère l’évènement créer avec l'id maximum et je l'affiche afin que l'utilisateur puisse le modifier à sa guise.
Là est le problème, mon for dans ma vue ne récupére pas le bon evenement puisque j'ai une erreur toute bidon :
Catchable Fatal Error: Argument 1 passed to monsite\CalendarBundle\Entity\Event::setDateBegin() must be an instance of DateTime, null given, called in C:\wamp\www\monsite\vendor\symfony\src\Symfony\Component\Form\Util\PropertyPath.php on line 347 and defined in C:\wamp\www\monsite\src\monsite\CalendarBundle\Entity\Event.php line 63
Je pense que se n'est pas du tout la meilleur solution. Que me proposeriez vous si vous aviez à faire ça ?
Bien cordialement,
Sylvain
PS : Mon calendrier est en javascript, je ne sais pas si ça peut vous influencer. Merci encore !
Partager