Yes, Doctrine supports this.

The ORM does not care about what your constructor looks like. I will never use it when restoring the entity from the database (it uses the Reflection API for that). I often have some mandatory controller arguments in my entities.

If your entity is immutable, there is even a performance optimization you can perform: mark the entity as readOnly in the Doctrine mapping. Read-only entities can be created and deleted, but the ORM will never check them for changes when doing a flush (and so won't ever perform an UPDATE on them). If your class is immutable, checking for changes is just a waste of CPU time. Note that the readOnly flag is a bad naming, as write operations can still be performed on the table corresponding to the entity (INSERT and DELETE). A proper naming for it would have been "immutable" instead.

--
Christophe | Stof


Le 08/09/2017 à 18:46, Dennis Matveyev a écrit :
I see.

Well, perhaps since my object already had an identity (by this I am assuming you are saying it has a unique identifier by which it is known in a database), I best keep it an entity (even if it is not yet maintained under Doctrine facilities).

Perhaps my struggle is something else. It is whether or not to make it immutable and if Doctrine works with that. The reason immutability is just that there is no reason for the object to change. And if it is indeed changed, a new one is created. Basically what I wanted to do is to initialize the object via its constructor, and was not sure if Doctrine allows for me to do this. Usually I have not used a constructor for anything when working with entities in Doctrine, and I don't know what Doctrine will do or what I should do if I do specify such a constructor.


On Friday, September 8, 2017 at 11:45:37 AM UTC-4, Maarten van Leeuwen wrote:

    I think you mix up some terminology: An object is an entity by
    nature if it is characterized and identified by its identity. In
    other words: if it has a primary key in the database it probably
    an entity and can be modelled into a Doctrine Entity.

    Value object ar characterized not by identity, but by value. A
    date or datetime is characterized by value, not by identity and
    those cannot be used as entities.


    Regards
    Maarten van Leeuwen

    Op 8 sep. 2017 om 17:11 heeft Dennis Matveyev <[email protected]
    <javascript:>> het volgende geschreven:

    In my code I currently have an active record implementation of a
    Staff object.  Namely, that object self-populates at creation
    time, and self-reloads when required.

    |
    classStaff
    {
    |private$id;
    | private$name;
        private $email;

        //all the getters and setters
    }
    |

    I changed the code where I pulled out database code out of it to
    where it now populates from the outside. One of the next steps I
    can take is make this object into a Doctrine Entity, which is a
    fairly-well understood concept to me (I have existing entities in
    my code)
    Before going that route, I thought .. what if I make it into an
    immutable value object.  Will it benefit me?

    And that is my question here.  Staff is used as part of a session
    and typically does not need to be mutable.
    As part of making it a value object I can _remove all the
    setters_ and _move all initialization and object-loading code
    into the constructor_.

    This is where I'm struggling ...  If I do this

      * can I still use the object with Doctrine?
      * Is doing this a good idea?


-- You received this message because you are subscribed to the
    Google Groups "doctrine-user" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to [email protected] <javascript:>.
    To post to this group, send email to [email protected]
    <javascript:>.
    Visit this group at https://groups.google.com/group/doctrine-user
    <https://groups.google.com/group/doctrine-user>.
    For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google Groups "doctrine-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>. To post to this group, send email to [email protected] <mailto:[email protected]>.
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to