Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity Listener documentation is incomplete #457

Closed
fredcooper opened this issue Aug 31, 2015 · 9 comments
Closed

Entity Listener documentation is incomplete #457

fredcooper opened this issue Aug 31, 2015 · 9 comments
Assignees
Milestone

Comments

@fredcooper
Copy link

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

@kimhemsoe
Copy link
Member

We always welcome pull requests :D

@trappar
Copy link

trappar commented Jan 12, 2016

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.

@Ocramius
Copy link
Member

@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.

@Ocramius Ocramius changed the title Entity Lisener documentation is uncomplete Entity Listener documentation is incomplete Jan 12, 2016
@trappar
Copy link

trappar commented Jan 12, 2016

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.

@Koc
Copy link
Contributor

Koc commented Nov 19, 2016

Can anybody take care about it?

// cc @kimhemsoe

@acidjames
Copy link

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 = 'foo@bar.com';
        $to = 'foo@bar.com';
        $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...

@slince
Copy link

slince commented Mar 10, 2018

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;

@slince
Copy link

slince commented Mar 10, 2018

@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 }

@alcaeus
Copy link
Member

alcaeus commented Feb 5, 2019

Documentation was improved in #906 and #910. Closing here.

@alcaeus alcaeus closed this as completed Feb 5, 2019
@alcaeus alcaeus self-assigned this Feb 5, 2019
@alcaeus alcaeus added this to the 1.10.2 milestone Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants