Symfony 3.4集成FullCalendarBundle无法加载自定义事件求助
Hey there, let's work through this step by step—since your calendar is rendering but not pulling in your custom Event entities, we can narrow down the issues in a few key areas:
1. Confirm Your Listener is Registered as a Service
Symfony 3.4 depends on explicit service configuration to recognize event listeners. Double-check that you’ve registered your LoadDataListener in app/config/services.yml (or XML) and tagged it correctly for the FullCalendar event.
Example services.yml entry:
services: app.fullcalendar.load_data_listener: class: AppBundle\Listener\LoadDataListener arguments: ['@doctrine.orm.entity_manager'] tags: - { name: kernel.event_listener, event: fullcalendar.set_data, method: loadData }
Make sure the event name matches exactly (fullcalendar.set_data) and the method name aligns with the one in your listener class (typically loadData).
2. Validate Your Listener's Load Logic
Let’s compare your listener code to a standard working implementation to catch gaps:
<?php namespace AppBundle\Listener; use AncaRebeca\FullCalendarBundle\Event\CalendarEvent as BundleCalendarEvent; use AncaRebeca\FullCalendarBundle\Model\Event; use Doctrine\ORM\EntityManagerInterface; class LoadDataListener { private $entityManager; public function __construct(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } public function loadData(BundleCalendarEvent $calendarEvent) { // Get date range sent by FullCalendar $startDate = $calendarEvent->getStart(); $endDate = $calendarEvent->getEnd(); // Fetch events from your custom entity (filter by date range!) $customEvents = $this->entityManager->getRepository('AppBundle:Event') ->createQueryBuilder('e') ->where('e.date BETWEEN :start AND :end') ->setParameter('start', $startDate->format('Y-m-d H:i:s')) ->setParameter('end', $endDate->format('Y-m-d H:i:s')) ->getQuery() ->getResult(); // Convert custom entities to FullCalendar events foreach ($customEvents as $event) { $fullCalendarEvent = new Event( $event->getTitle(), $event->getDate() // Ensure this returns a \DateTime object! ); // Optional: Add extra properties (ID, color, URL) $fullCalendarEvent->setId($event->getId()); // $fullCalendarEvent->setColor('#2ecc71'); // Critical: Add the event to the calendar event collection $calendarEvent->addEvent($fullCalendarEvent); } } }
Key checks here:
- Are you filtering events by the
startDateandendDateprovided by FullCalendar? Skipping this can lead to no events being returned if your query doesn’t match the calendar’s visible range. - Does your entity’s
getDate()method return a\DateTimeobject? If it’s stored as a string, you’ll need to convert it before passing to the FullCalendarEventclass. - Did you remember to call
$calendarEvent->addEvent($fullCalendarEvent)? It’s an easy step to miss!
3. Check Entity Mapping & Repository
Ensure your AppBundle:Event entity has correct Doctrine mappings and working getters:
<?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="AppBundle\Repository\EventRepository") */ class Event { /** * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $title; /** * @ORM\Column(type="datetime") */ private $date; // Required getters public function getTitle(): string { return $this->title; } public function getDate(): \DateTimeInterface { return $this->date; } // Add setters if needed... }
Test your repository directly (e.g., in a controller) to confirm it returns events:
// In a test controller action $events = $this->getDoctrine()->getRepository('AppBundle:Event')->findAll(); var_dump($events); // Should show your custom events
4. Verify Frontend Event Fetching
Make sure your FullCalendar initialization points to the correct route. The bundle provides a default fullcalendar_load_events route—check your Twig template’s JS:
$(document).ready(function() { $('#calendar').fullCalendar({ events: { url: '{{ path('fullcalendar_load_events') }}', type: 'POST', error: function() { alert('Failed to load events!'); } }, // Other FullCalendar options... }); });
Open your browser’s Network tab and inspect the request to fullcalendar_load_events:
- Is it returning a 200 OK status?
- Does the response JSON contain your event data? If not, check for server errors (500 status) in your Symfony logs.
5. Debug Listener Execution
To confirm your listener is running, add a debug statement inside the loadData method:
public function loadData(BundleCalendarEvent $calendarEvent) { var_dump('Listener triggered!'); die; // Rest of your code... }
If you don’t see this message when loading the calendar, your listener isn’t registered correctly—go back to step 1 and recheck your service tags.
内容的提问来源于stack exchange,提问作者Patmar

