Skip to content

Commit aeb35c0

Browse files
committed
First commit
0 parents  commit aeb35c0

24 files changed

+1752
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; top-most EditorConfig file
2+
root = true
3+
4+
; Unix-style newlines
5+
[*]
6+
end_of_line = LF
7+
insert_final_newline = true
8+
indent_style = space
9+
indent_size = 4

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
composer.lock
2+
.php_cs.cache
3+
.phpunit.result.cache
4+
vendor/

.php_cs.dist

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
$fileHeaderComment = <<<COMMENT
4+
This file is part of the doctrine-orm-refetch package.
5+
6+
(c) E-commit <[email protected]>
7+
8+
For the full copyright and license information, please view the LICENSE
9+
file that was distributed with this source code.
10+
COMMENT;
11+
12+
$finder = PhpCsFixer\Finder::create()
13+
->in(__DIR__)
14+
;
15+
16+
return PhpCsFixer\Config::create()
17+
->setRiskyAllowed(true)
18+
->setRules([
19+
'@Symfony' => true,
20+
'@Symfony:risky' => true,
21+
'@DoctrineAnnotation' => true,
22+
'@PHP56Migration' => true,
23+
'@PHP56Migration:risky' => true,
24+
'@PHP71Migration' => true,
25+
'@PHP71Migration:risky' => true,
26+
'@PHP73Migration' => true,
27+
'array_syntax' => ['syntax' => 'short'],
28+
'fopen_flags' => true,
29+
'header_comment' => ['header' => $fileHeaderComment, 'separate' => 'both'],
30+
'linebreak_after_opening_tag' => true,
31+
'mb_str_functions' => true,
32+
'no_php4_constructor' => true,
33+
'no_unreachable_default_argument_value' => true,
34+
'no_useless_else' => true,
35+
'no_useless_return' => true,
36+
'ordered_imports' => true,
37+
'phpdoc_order' => true,
38+
'protected_to_private' => false,
39+
])
40+
->setFinder($finder)
41+
;

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
php:
3+
- 7.2
4+
- 7.3
5+
- 7.4
6+
7+
env:
8+
- DEPENDENCIES="high"
9+
- DEPENDENCIES="low"
10+
11+
install:
12+
- if [ "$DEPENDENCIES" = "low" ]; then composer update --prefer-lowest --prefer-stable; else composer install; fi;
13+
14+
script:
15+
- php vendor/phpunit/phpunit/phpunit
16+
- php vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --diff --dry-run -v

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 E-COMMIT
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Doctrine ORM Refetch
2+
3+
This library allows to re-fetch Doctrine ORM objects after clear the object manager.
4+
5+
[![Build Status](https://travis-ci.org/e-commit/doctrine-orm-refetch.svg?branch=master)](https://travis-ci.org/e-commit/doctrine-orm-refetch)
6+
7+
## Installation ##
8+
9+
To install doctrine-orm-refetch with Composer just run :
10+
11+
```bash
12+
$ composer require ecommit/doctrine-orm-refetch:1.*@dev
13+
```
14+
15+
## Usage ##
16+
17+
Create the utility (`$entityManager` is the Doctrine ORM entity manager):
18+
19+
```php
20+
use Ecommit\DoctrineOrmRefetch\RefetchManager;
21+
22+
$refetchManager = Ecommit\DoctrineOrmRefetch\RefetchManager::create($entityManager);
23+
```
24+
25+
### Refetch an object ###
26+
27+
```php
28+
$myObject = $refetchManager->getFetchedObject($myObject);
29+
//or $refetchManager->refreshObject($myObject);
30+
```
31+
32+
Example:
33+
34+
```php
35+
use Ecommit\DoctrineOrmRefetch\RefetchManager;
36+
37+
$refetchManager = RefetchManager::create($entityManager);
38+
39+
$author = $entityManager->getRepository(Author::class)->find(1);
40+
41+
$queryBuilder = $entityManager->getRepository(Book::class)->createQueryBuilder('b');
42+
$queryBuilder->select('b')
43+
->andWhere('b.bookId != :bookId')
44+
->setParameter('bookId', 7);
45+
$iterableResult = $queryBuilder->getQuery()->iterate();
46+
47+
$i = 0;
48+
foreach ($iterableResult as $row) {
49+
++$i;
50+
$book = current($row);
51+
52+
if (!$book->getAuthors()->contains($author)) {
53+
$book->addAuthor($author);
54+
}
55+
56+
if (0 === $i % 20) {
57+
//$author is managed
58+
$entityManager->flush();
59+
$entityManager->clear();
60+
//$author is not managed
61+
$author = $refetchManager->getObject($author);
62+
//$author is managed
63+
}
64+
}
65+
66+
$entityManager->flush();
67+
$entityManager->clear();
68+
```
69+
70+
### Get collection by critera ###
71+
72+
```php
73+
$collection = $refetchManager->getCollectionFromCriteria($criteria, 'MyClass');
74+
```
75+
76+
Example:
77+
78+
```php
79+
use Ecommit\DoctrineOrmRefetch\RefetchManager;
80+
81+
$refetchManager = RefetchManager::create($entityManager);
82+
83+
$ctiteria = Criteria::create()
84+
->andWhere(Criteria::expr()->gt('authorId', 2));
85+
$authors = $refetchManager->getCollectionFromCriteria($ctiteria, Author::class);
86+
87+
$queryBuilder = $entityManager->getRepository(Book::class)->createQueryBuilder('b');
88+
$queryBuilder->select('b')
89+
->andWhere('b.bookId != :bookId')
90+
->setParameter('bookId', 9);
91+
$iterableResult = $queryBuilder->getQuery()->iterate();
92+
93+
$i = 0;
94+
foreach ($iterableResult as $row) {
95+
++$i;
96+
$book = current($row);
97+
98+
foreach ($authors as $author) {
99+
if (!$book->getAuthors()->contains($author)) {
100+
$book->addAuthor($author);
101+
}
102+
}
103+
104+
if (0 === $i % 20) {
105+
$entityManager->flush();
106+
$entityManager->clear();
107+
$authors = $refetchManager->getCollectionFromCriteria($ctiteria, Author::class);
108+
}
109+
}
110+
111+
$entityManager->flush();
112+
$entityManager->clear();
113+
```
114+
115+
116+
## License ##
117+
118+
This librairy is under the MIT license. See the complete license in *LICENSE* file.

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "ecommit/doctrine-orm-refetch",
3+
"description": "Refetch ORM Doctrine objects.",
4+
"keywords": ["doctrine", "refetch"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Hubert Lecorche",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": { "Ecommit\\DoctrineOrmRefetch\\": "src/" }
15+
},
16+
"autoload-dev": {
17+
"psr-4": { "Ecommit\\DoctrineOrmRefetch\\Tests\\": "tests/" }
18+
},
19+
"require": {
20+
"php": "^7.2",
21+
"doctrine/common": "^2.11",
22+
"doctrine/orm": "^2.7"
23+
},
24+
"require-dev": {
25+
"doctrine/data-fixtures": "^1.4.1",
26+
"friendsofphp/php-cs-fixer": "^2.16.1",
27+
"fzaninotto/faker": "^1.9",
28+
"phpunit/phpunit": "^8.4"
29+
},
30+
"extra": {
31+
"branch-alias": {
32+
"dev-master": "1.0.x-dev"
33+
}
34+
},
35+
"config": {
36+
"sort-packages": true
37+
}
38+
}

phpunit.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.0/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
beStrictAboutTestsThatDoNotTestAnything="false"
9+
bootstrap="tests/phpunit.bootstrap.php"
10+
>
11+
12+
<testsuites>
13+
<testsuite name="Project Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the doctrine-orm-refetch package.
7+
*
8+
* (c) E-commit <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ecommit\DoctrineOrmRefetch\Exception;
15+
16+
class EntityNotFoundException extends \Exception implements ExceptionInterface
17+
{
18+
public static function fromClassNameAndIdentifier($className, array $id)
19+
{
20+
$ids = [];
21+
22+
foreach ($id as $key => $value) {
23+
$ids[] = $key.'('.$value.')';
24+
}
25+
26+
return new self(
27+
'Entity of type \''.$className.'\''.($ids ? ' for IDs '.implode(', ', $ids) : '').' was not found'
28+
);
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the doctrine-orm-refetch package.
7+
*
8+
* (c) E-commit <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ecommit\DoctrineOrmRefetch\Exception;
15+
16+
interface ExceptionInterface
17+
{
18+
}

0 commit comments

Comments
 (0)