![]() ![]() If you use Doctrine and want to copy an entity, you will need to use the DoctrineCollectionFilter: use DeepCopy\ DeepCopy $copier-> addFilter( new ChainableFilter( new DoctrineProxyFilter()), new DoctrineProxyMatcher()) Įcho $copy-> id // null DoctrineCollectionFilter (filter) Use DeepCopy\ Matcher\ Doctrine\ DoctrineProxyMatcher Use DeepCopy\ Filter\ Doctrine\ DoctrineProxyFilter You can use the ChainableFilter as a decorator of the proxy loader filter, which won't stop the chain of filters (i.e. If you use cloning on proxy classes, you might want to apply two filters for: $copy->category has not been touched ChainableFilter (filter) $copier-> addFilter( new KeepFilter(), new PropertyMatcher( 'M圜lass', 'category')) If you want a property to remain untouched (for example, an association to an object): use DeepCopy\ DeepCopy $copier-> addFilter( new SetNullFilter(), new PropertyNameMatcher( 'id')) Įcho $copy-> id // null KeepFilter (filter) Use DeepCopy\ Matcher\ PropertyNameMatcher Įcho $object-> id // 123 $copier = new DeepCopy() Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have Using the ( ChainableFilter) won't stop the chain of filters. DeepCopy\TypeFilter applies a transformation to any element matched by DeepCopy\TypeMatcherīy design, matching a filter will stop the chain of filters (i.e.DeepCopy\Filter applies a transformation to the object attribute matched by DeepCopy\Matcher.Will apply a filter to any object that is an instance of Doctrine\Common\Collections\Collection $matcher = new TypeMatcher( 'Doctrine\Common\Collections\Collection') Filters Gettype() function): use DeepCopy\ TypeMatcher\ TypeMatcher The TypeMatcher will match any element by its type (instance of a class or any value that could be parameter of Will apply a filter to the property "id" of any objects of the class "M圜lass" $matcher = new PropertyMatcher( 'M圜lass', 'id') Type The PropertyMatcher will match a specific property of a specific class: use DeepCopy\ Matcher\ PropertyMatcher Will apply a filter to any property of any objects named "id" $matcher = new PropertyNameMatcher( 'id') Specific property The PropertyNameMatcher will match a property by its name: use DeepCopy\ Matcher\ PropertyNameMatcher DeepCopy\TypeMatcher applies on any element found in graph, including array elements.DeepCopy\Matcher applies on a object attribute. ![]() We provide some generic filters and matchers. With $filter implementing DeepCopy\Filter\FilterĪnd $matcher implementing DeepCopy\Matcher\Matcher. The method to add a filter is DeepCopy\DeepCopy::addFilter($filter, $matcher), You can add filters to customize the copy process. You may want to roll your own deep copy function: namespace Acme To use it: use function DeepCopy\deep_copy Īlternatively, you can create your own DeepCopy instance to configure it differently for example: use DeepCopy\ DeepCopy Keeps a hash map of all instances and thus preserves the object graph. To avoid cloning the same object twice it But how do you handle cycles in the association graph?ĭeepCopy recursively traverses all the object's properties and clones them.You use _clone() and implement the behavior copying also all the objects referenced in the properties)? How do you create deep copies of your objects (i.e.How do you create copies of your objects?.Install with Composer: composer require myclabs/deep-copy It is designed to handle cycles in the association graph. DeepCopy helps you create deep copies (clones) of your objects. ![]()
0 Comments
Leave a Reply. |