The documentation about entity listener is obviously uncomplete. There is a missing part. I would really enjoy being able to use the freaking new feature
We always welcome pull requests :D
Asking a normal user to provide documentation for something he didn't write is kind of ridiculous. The documentation is still lacking and this feature just doesn't seem to work at all. Really frustrating.
@trappar I re-state what @kimhemsoe: patches welcome.
"normal user" doesn't mean "we get stuff done by others".
While I understand this frustration of yours, this is not the tone with which you can ask for fixes.
I mean no offense, it just seems to me pretty ridiculous to tell him to fix the problem himself when he doesn't even understand what the problem is.
Also, pretty much everyone welcomes pull requests, that's not the point of issues. Issues are for notifying people who might be in a position to fix things that there are things to be fixed. If no one has time to fix it then so be it, but don't shoot the messenger.
Can anybody take care about it?
// cc @kimhemsoe
I've followed the doc, for those who are still struggling.
Symfony 2.8, Doctrine 2.4
For example, my test sends a mail when a doctrine entity is updated.
services.yml
acme.document_listener:
class: Acme\BlogBundle\Entity\Listener\DocumentListener
arguments: [@acme.mailer]
tags:
- { name: doctrine.orm.entity_listener, lazy: true }
acme.mailer:
class: Acme\BlogBundle\Mailer\Mailer
arguments: [@mailer, @templating]
The mailer service i created needs the templating service to render twig in emails. You need to register the Listener in the doctrine entity listener event cycle.
DocumentListener.php
<?php
namespace Acme\BlogBundle\Entity\Listener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Acme\BlogBundle\Entity\Document;
use Acme\BlogBundle\Mailer\Mailer;
class DocumentListener
{
protected $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function postUpdate(Document $document, LifecycleEventArgs $event){
$this->mailer->sendTestMessage();
}
}
My mailer is just a simple mailer :
<?php
namespace Acme\BlogBundle\Mailer;
use Symfony\Component\Templating\EngineInterface;
class Mailer
{
protected $mailer;
protected $templating;
public function __construct(\Swift_Mailer $mailer, EngineInterface $templating)
{
$this->mailer = $mailer;
$this->templating = $templating;
}
public function sendTestMessage()
{
$template = 'AcmeBlogBundle:Mail:test.html.twig';
$from = '[email protected]';
$to = '[email protected]';
$subject = 'Testing email yay';
$body = $this->templating->render($template, array());
$this->sendMessage($from, $to, $subject, $body);
}
protected function sendMessage($from, $to, $subject, $body)
{
$mail = \Swift_Message::newInstance();
$mail
->setFrom($from)
->setTo($to)
->setSubject($subject)
->setBody($body)
->setContentType('text/html');
$this->mailer->send($mail);
}
}
And in the Document Entity, don't forget the annotation
<?php
namespace Acme\BlogBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Document
*
* @ORM\Table()
* @ORM\Entity()
* @ORM\EntityListeners({"Acme\BlogBundle\Entity\Listener\DocumentListener"})
* @Vich\Uploadable()
*/
class Document
{
etc...
Hello everyone,
I have read this document http://symfony.com/doc/current/bundles/DoctrineBundle/entity-listeners.html; Actually I don't even know what other parameters this tag has; I have tried to find the answer myself , but I do not know how to search for my problem, because I do not know what keywords to use; so I agree with @trappar , I am willing to submit pr, but the premise is that I know how to solve the problem;
@acidjames
Hi, you can try this;
acme.document_listener:
class: Acme\BlogBundle\Entity\Listener\DocumentListener
arguments: [@acme.mailer]
tags:
- { name: doctrine.orm.entity_listener, entity: Acme\BlogBundle\Entity\Document, event: postUpdate, method: postUpdate }
Documentation was improved in #906 and #910. Closing here.
Most helpful comment
@trappar I re-state what @kimhemsoe: patches welcome.
"normal user" doesn't mean "we get stuff done by others".
While I understand this frustration of yours, this is not the tone with which you can ask for fixes.