The easiest way is to have a `copy()` method (note that it is not
`__clone`) that does this in the entity.

You just handle the copying in the entity itself, by creating a new
instance and associating it, and then setting the name or whatever
properties need updating.

Alternatively, an external service can also do the same thing (and also
persist the newly created entity).

Interfacing this? Not sure if it's worth it - depends on your use-case.


Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 28 April 2014 23:19, Jessica Mauerhan <[email protected]> wrote:

> I have several different entities that I want to make "Copyable" in my
> application, and define both properties with default values and methods
> with implementation. I am confused about how to go about this, using Traits
> vs. Interfaces, and how to get Doctrine to properly associate the entity to
> its "original" entity.
>
> So for example, I have an Item, and when we copy an item (the users then
> edit the new version, but we want to know which one it was originally
> copied from) I want to automatically add some text to the end of it's name.
> Another entity will have different text added to the end of a different
> field, but basically anything that can be copied shares this behavior that
> when you copy it, some text is added to the end of a designated field, so I
> want two properties (for the text, and the field) and then a property for
> the original id.
>
> I need to be able to define the copiedFrom association for these
> entities, but if I use a trait, I cannot override the Doctrine association
> later, and if I use an interface, I can't define the Doctrine association
> to begin with, only the methods such as getFieldToChange(), etc.
>
> I tried setting it up as a trait where the $copiedFrom property simply
> has no mapping, and then set up an association override in the model that
> uses the trait, but this appears to create an infinite loop of some sort,
> and we run out of memory.
>
>  * @AssociationOverrides({
>  *      @AssociationOverride(name="copiedFrom",
>  *          joinColumns=@JoinColumn(
>  *              name="copied_from_id", referencedColumnName="id"
>  *          )
>  *      )
>  * })
>
> Is there another way to approach this, perhaps using a custom annotation,
> or some other design??
>
> What I have done in order to get it to function for now is remove the
> $copiedFrom property from the trait, and manually define it in each class
> that uses Copyable, however I'm pretty sure this is not the "right" way.
>
>
> (x-post to SO)
>
> --
> 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 http://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 http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to