1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Id; |
21
|
|
|
|
22
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* IncrementGenerator is responsible for generating auto increment identifiers. It uses |
26
|
|
|
* a collection and generates the next id by using $inc on a field named "current_id". |
27
|
|
|
* |
28
|
|
|
* The 'collection' property determines which collection name is used to store the |
29
|
|
|
* id values. If not specified it defaults to 'doctrine_increment_ids'. |
30
|
|
|
* |
31
|
|
|
* The 'key' property determines the document ID used to store the id values in the |
32
|
|
|
* collection. If not specified it defaults to the name of the collection for the |
33
|
|
|
* document. |
34
|
|
|
* |
35
|
|
|
* @since 1.0 |
36
|
|
|
*/ |
37
|
|
|
class IncrementGenerator extends AbstractIdGenerator |
38
|
|
|
{ |
39
|
|
|
protected $collection = null; |
40
|
|
|
protected $key = null; |
41
|
|
|
protected $startingId = 1; |
42
|
|
|
|
43
|
|
|
public function setCollection($collection) |
44
|
|
|
{ |
45
|
|
|
$this->collection = $collection; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setKey($key) |
49
|
|
|
{ |
50
|
|
|
$this->key = $key; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function setStartingId($startingId) |
54
|
|
|
{ |
55
|
2 |
|
$this->startingId = $startingId; |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** @inheritDoc */ |
59
|
11 |
|
public function generate(DocumentManager $dm, $document) |
60
|
|
|
{ |
61
|
11 |
|
$className = get_class($document); |
62
|
11 |
|
$db = $dm->getDocumentDatabase($className); |
63
|
|
|
|
64
|
11 |
|
$coll = $this->collection ?: 'doctrine_increment_ids'; |
65
|
11 |
|
$key = $this->key ?: $dm->getDocumentCollection($className)->getName(); |
66
|
|
|
|
67
|
|
|
/* |
68
|
|
|
* Unable to use '$inc' and '$setOnInsert' together due to known bug. |
69
|
|
|
* @see https://jira.mongodb.org/browse/SERVER-10711 |
70
|
|
|
* Results in error: Cannot update 'current_id' and 'current_id' at the same time |
71
|
|
|
*/ |
72
|
|
|
$command = [ |
73
|
11 |
|
'findAndModify' => $coll, |
74
|
11 |
|
'query' => ['_id' => $key, 'current_id' => ['$exists' => true]], |
75
|
11 |
|
'update' => ['$inc' => ['current_id' => 1]], |
76
|
11 |
|
'upsert' => false, |
77
|
11 |
|
'new' => true, |
78
|
11 |
|
]; |
79
|
11 |
|
$result = $db->command($command); |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* Updated nothing - counter doesn't exist, creating new counter. |
83
|
|
|
* Not bothering with {$exists: false} in the criteria as that won't avoid |
84
|
|
|
* an exception during a possible race condition. |
85
|
|
|
*/ |
86
|
11 |
|
if (array_key_exists('value', $result) && ! isset($result['value'])) { |
87
|
|
|
$command = [ |
88
|
11 |
|
'findAndModify' => $coll, |
89
|
11 |
|
'query' => ['_id' => $key], |
90
|
11 |
|
'update' => ['$inc' => ['current_id' => $this->startingId]], |
91
|
11 |
|
'upsert' => true, |
92
|
11 |
|
'new' => true, |
93
|
11 |
|
]; |
94
|
11 |
|
$db->command($command); |
95
|
11 |
|
return $this->startingId; |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
return $result['value']['current_id']; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|