Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RFC] [Workflow] WIP Create MarkingHistoryStore to allow logging of an entity's states in the entity record #28265 #28266

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

Conversation

ChangePlaces
Copy link

Q A
Branch? master
Bug fix? no
New feature? yes (changelog not updated)
BC breaks? no
Deprecations? no
Tests pass? todo
Fixed tickets #28253
License MIT
Doc PR todo

see #28253

This adds the feature for entities with workflows to keep a log of its previous states along with a timestamp.

@ChangePlaces ChangePlaces requested a review from lyrixx as a code owner August 26, 2018 01:41
@ChangePlaces ChangePlaces changed the title [Workflow] WIP Create MarkingHistoryStore to allow logging of an entity's states #28265 [Workflow] [RFC] WIP Create MarkingHistoryStore to allow logging of an entity's states #28265 Aug 26, 2018
@ChangePlaces ChangePlaces changed the title [Workflow] [RFC] WIP Create MarkingHistoryStore to allow logging of an entity's states #28265 [RFC] [Workflow] WIP Create MarkingHistoryStore to allow logging of an entity's states #28265 Aug 26, 2018
@ChangePlaces ChangePlaces changed the title [RFC] [Workflow] WIP Create MarkingHistoryStore to allow logging of an entity's states #28265 [RFC] [Workflow] WIP Create MarkingHistoryStore to allow logging of an entity's states in the entity record #28265 Aug 26, 2018
@nicolas-grekas nicolas-grekas added this to the next milestone Aug 26, 2018
@Padam87
Copy link
Contributor

Padam87 commented Aug 26, 2018

IMO this is a bit too specific functionality, and the implementation is very opinionated.

Maybe create a bundle instead?

I use this in my projects:

class WorkflowLogListener implements EventSubscriberInterface
{
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return [
            'workflow.completed' => 'completed',
        ];
    }

    public function completed(Event $event)
    {
        $subject = $event->getSubject();

        $log = new WorkflowLog();
        $log
            ->setSubjectClass(get_class($subject))
            ->setSubjectId($subject->getId())
            ->setWorkflow($event->getWorkflowName())
            ->setTransition($event->getTransition()->getName())
            ->setFroms($event->getTransition()->getFroms())
            ->setTos($event->getTransition()->getTos())
        ;

        $this->em->persist($log);
        $this->em->flush();
    }
}

@ChangePlaces
Copy link
Author

Sure, but that isn't on the entity itself

@Padam87
Copy link
Contributor

Padam87 commented Aug 26, 2018

Not sure why that is a problem. Create a repository, add a method to select based on the entity(class and id), use that in the controller to fetch results.

@ChangePlaces
Copy link
Author

ChangePlaces commented Aug 26, 2018

or put three extra lines in your workflow config. If you need more detailed log, then create your own listener.

Your solution poses some problems:

  • you have to log every transition change across ALL of your workflows, even on a workflow you may not want to log. That could be a lot of wasted database rows
  • event listener priority: by having a listener to store a log entity it makes it more difficult for logic to fire based on an entity's history (does the listener that checks the logic fire after the log listener, or before or?) Incidently this is how I first solved the need too but the priority was an ugly issue. However, using this way, you can have a listener for the entered event and know that the history has been set.

@noniagriconomie
Copy link
Contributor

noniagriconomie commented Nov 19, 2018

Hi, generaly a workflow state is binded to a status of an entity

I do something like this on most of my workflow entities when i want to track status change on specific entity, to answer your Sure, but that isn't on the entity itself

    // the setStatus is called by our implementation of MarkingStoreInterface
    public function setStatus(int $status): self
    {
        $this->status = $status;
        $this->statusLogRecords->add(new MyEntityLog($this));

        return $this;
    }

And the MyEntityLog have properties like the status (taken from $this), a Gedmo Timestampable (on create) and Blameable (on create), the target entity etc

This PR #29146 will also allow us to add some context, like a comment, to this log if you need documentation or whatever when the state changes :)

@lyrixx
Copy link
Member

lyrixx commented Nov 19, 2018

I totally agree with @noniagriconomie
This is also the solution in mentioned in #28253 (comment)

I'm really sorry to close your first PR on Symfony, but I can not merge this PR because we are several to prefer the solution proposed by @noniagriconomie

Anyway, Thanks for your work.

@lyrixx lyrixx closed this Nov 19, 2018
@stof stof removed this from the next milestone Nov 23, 2018
lyrixx added a commit that referenced this pull request Mar 6, 2019
…ixx)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Workflow] Added a context to `Workflow::apply()`

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27925 (maybe #28253 and #28266)
| License       | MIT
| Doc PR        |

Commits
-------

7d5b7a3 [Workflow] Added a context to `Workflow::apply()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants