Sorry, I guess I didn't explain it well - the copying is not the problem, we already have all the code that copies each Entity and it works great.
The problem is that for any of the entities that are "Copyable" they need 3 additional properties, one of which is a Doctrine association back to the original version. So here is my Copyable trait: http://pastebin.com/sqmvGPp3 You'll see the default for the field name is 'name', and the default text to add is ' (COPY)'. The $copiedFrom association I have not defined in the trait, because then I cannot override it in the models that use the trait. So then I have two models using the trait, one called Promo and for Promo I have to manually define the copiedFrom like this: /** * @var Promo * * @ORM\ManyToOne(targetEntity="models\Promo", cascade={"all"}) * @ORM\JoinColumns({ * @ORM\JoinColumn(name="copied_from_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") * }) */ protected $copiedFrom; Then since this is the model that differs from the usual name/(COPY) I have in the constructor: public function __construct() { $this->promoItems = new ArrayCollection(); $this->orders = new ArrayCollection(); $this->fieldToIncrementWhenCopied = 'code'; $this->copyAppender = 1; } The logic that handles the new properties is already in place and works. My question is about the right way to ensure all Copyable entities have these three properties, and getting that copiedFrom association defined - right now, it's possible for a developer to create an entity that uses the Copyable trait, and forget to add the $copiedFrom association, which (a) breaks auto-generated doctrine migration diff, (b) means we have to have checks in the code that throw an exception later when you try to copy it. On Tuesday, April 29, 2014 6:31:40 AM UTC-5, Marco Pivetta wrote: > > 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]<javascript:> > > 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] <javascript:>. >> To post to this group, send email to [email protected]<javascript:> >> . >> 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.
