yohang / Finite

A Simple PHP Finite State Machine

Home Page:http://yohan.giarel.li/Finite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

StateMachine used with Doctrine Entities

sagikazarmark opened this issue · comments

I would like to use a StateMachine implementation with some of my entities, but it seems a bit weird that the statemachine is not injected into the entity, but the entity is injected into the statemachine (maybe because this is new to me).

Any suggestion how to use this lib with doctrine? There is a doctrine behavior implementation, but I would be happier with a doctrine type instead.

a doctrine class is just a php class. This is the best part of doctrine.
So it's not a service, nothing get injected inside a POPO. In fact the state machine use a property of the entity (that eventually gets persisted to the db) so it's ok that you pass the "subject" object to the state machine service. It wouldn't make any sense to the inverse. Well, in fact it's impossible...unless you use a postLoad event...maybe...but don't do that.

I thought something similar. However I would be happy to see some real world examples (either with doctrine or not) with some context (so actually using it as a service)

IMO this is the opposite direction of which this lib should go. The state machine should be "stateless", eliminating the need to create a new (or reinit an existing) instance every time you need to be working with more than one subject.

Hi,

StateMachine won't be injected in your entities, or more globally, state-controlled objects.

Your graph logic has nothing to do in your entities, which are, essentially, data-carrying objects.

If you really want to inject StateMachine into Doctrine object, you can do that with a postLoad lifecycle event, but honestly, don't do this.