Skip to content

Commit 5a5a9fc

Browse files
authored
Merge pull request #59 from malc0mn/master
Added doctrine Proxy matcher/filter
2 parents 399c1f9 + 1f2c776 commit 5a5a9fc

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ $myCopy = $deepCopy->copy($myObject);
258258
// $myCopy->myProperty will return an empty collection
259259
```
260260

261+
#### `DoctrineProxyFilter`
262+
263+
If you use Doctrine and use cloning on lazy loaded entities, you might encounter errors mentioning missing fields on a
264+
Doctrine proxy class (...\\\_\_CG\_\_\Proxy).
265+
You can use the `DoctrineProxyFilter` to load the actual entity behind the Doctrine proxy class.
266+
**Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded before other filters are applied!**
267+
268+
```php
269+
use DeepCopy\DeepCopy;
270+
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
271+
use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
272+
273+
$deepCopy = new DeepCopy();
274+
$deepCopy->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher());
275+
$myCopy = $deepCopy->copy($myObject);
276+
277+
// $myCopy should now contain a clone of all entities, including those that were not yet fully loaded.
278+
```
279+
261280
## Contributing
262281

263282
DeepCopy is distributed under the MIT license.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace DeepCopy\Filter\Doctrine;
4+
5+
use DeepCopy\Filter\Filter;
6+
7+
/**
8+
* Trigger the magic method __load() on a Doctrine Proxy class to load the
9+
* actual entity from the database.
10+
*/
11+
class DoctrineProxyFilter implements Filter
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
public function apply($object, $property, $objectCopier)
17+
{
18+
$object->__load();
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace DeepCopy\Matcher\Doctrine;
4+
5+
use DeepCopy\Matcher\Matcher;
6+
use Doctrine\Common\Persistence\Proxy;
7+
8+
/**
9+
* Match a Doctrine Proxy class.
10+
*/
11+
class DoctrineProxyMatcher implements Matcher
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
public function matches($object, $property)
17+
{
18+
return $object instanceof Proxy;
19+
}
20+
}

0 commit comments

Comments
 (0)