Closed glouton closed 11 years ago
You need to define your service passing the DocumentManager as an argument
<service id="acme.demobundle.calendar_listener" class="Acme\DemoBundle\EventListener\CalendarEventListener">
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<tag name="kernel.event_listener" event="calendar.load_events" method="loadEvents" />
</service>
Hello, Thanks for your answer.
I think I did:
parameters:
calendar_event_listener.class: TheScienceTour\MainBundle\EventListener\CalendarEventListener
services:
acme.mybundle.calendar_listener:
class: "%calendar_event_listener.class%"
arguments: ["@doctrine_mongodb.odm.default_document_manager"]
tags:
- { name: kernel.event_listener, event: calendar.load_events, method: loadEvents }
Can I see your event listener class?
Sure thing, here it is:
<?php
namespace Acme\MyBundle\EventListener;
use ADesigns\CalendarBundle\Event\CalendarEvent;
use ADesigns\CalendarBundle\Entity\EventEntity;
use Doctrine\ODM\MongoDB\DocumentManager;
class CalendarEventListener {
private $dm;
public function __construct(DocumentManager $dm)
{
$this->dm = $dm;
}
public function loadEvents(CalendarEvent $calendarEvent)
{
$startDate = $calendarEvent->getStartDatetime();
$endDate = $calendarEvent->getEndDatetime();
// load events using your custom logic here,
// for instance, retrieving events from a repository
$myEvents = $this->dm->getRepository('AcmeEventBundle:Event')
->findAll();
foreach($myEvents as $myEvent) {
$myEventStartDate = new \DateTime();
$myEventStartDate->setTimestamp($myEvent->getStartDate()->sec);
// create an event with a start/end time, or an all day event
if ($myEvent->getAllDayEvent() === false) {
$myEventEndDate = new \DateTime();
$myEventEndDate->setTimestamp($myEvent->getEndDate()->sec);
$eventEntity = new EventEntity($myEvent->getTitle(), $myEventStartDate, $myEventEndDate);
} else {
$eventEntity = new EventEntity($myEvent->getTitle(), $myEventStartDate, null, true);
}
//optional calendar event settings
// $eventEntity->setAllDay(true); // default is false, set to true if this is an all day event
// $eventEntity->setBgColor('#FF0000'); //set the background color of the event's label
// $eventEntity->setFgColor('#FFFFFF'); //set the foreground color of the event's label
// $eventEntity->setUrl('http://www.google.com'); // url to send user to when event label is clicked
// $eventEntity->setCssClass('my-custom-class'); // a custom class you may want to apply to event labels
//finally, add the event to the CalendarEvent for displaying on the calendar
$calendarEvent->addEvent($eventEntity);
}
}
}
I've used a findAll() here just for testing but will get events between startDate and endDate. :)
It sounds like you are constructing an event listener from somewhere else in your application, or you have a typo somewhere. This bundle does not care or know about the event listeners you create. It simply dispatches an event, and then handles the results. It sounds like this problem is outside of the scope of this bundle.
It is indeed outside the bundle itself. I've followed the Readme.md and construted this listener in a bundle. The service is listed in "php app/console container:debug" so it seems like the document manager is injected in the listener constructor but isn't instantiate or null or something. :(
I'll keep searching, thanks anyway.
Yeah, I'm sorry I can't be of more help. I'm assuming you cleared your cache, and did a find in files to ensure you're not trying to instantiate this class from somewhere else?
Hello there,
Thanks for this bundle but I can't make it work.
I've injected the DocumentManager service but I have an internal server erreur:
PHP Catchable fatal error: Argument 1 passed to Acme\MyBundle\EventListener\CalendarEventListener::__construct() must be an instance of Doctrine\ODM\MongoDB\DocumentManager, none given
I've tried to change the route format according to the "How to define controlleers as services" http://symfony.com/doc/current/cookbook/controller/service.html but still the same error.
Any idea ?