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
|
|
|
|
42
|
|
|
public function setCollection($collection) |
43
|
|
|
{ |
44
|
|
|
$this->collection = $collection; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setKey($key) |
48
|
|
|
{ |
49
|
|
|
$this->key = $key; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @inheritDoc */ |
53
|
7 |
|
public function generate(DocumentManager $dm, $document) |
54
|
|
|
{ |
55
|
7 |
|
$className = get_class($document); |
56
|
7 |
|
$db = $dm->getDocumentDatabase($className); |
57
|
|
|
|
58
|
7 |
|
$coll = $this->collection ?: 'doctrine_increment_ids'; |
59
|
7 |
|
$key = $this->key ?: $dm->getDocumentCollection($className)->getName(); |
60
|
|
|
|
61
|
7 |
|
$query = array('_id' => $key); |
62
|
7 |
|
$newObj = array('$inc' => array('current_id' => 1)); |
63
|
|
|
|
64
|
7 |
|
$command = array(); |
65
|
7 |
|
$command['findandmodify'] = $coll; |
66
|
7 |
|
$command['query'] = $query; |
67
|
7 |
|
$command['update'] = $newObj; |
68
|
7 |
|
$command['upsert'] = true; |
69
|
7 |
|
$command['new'] = true; |
70
|
7 |
|
$result = $db->command($command); |
71
|
7 |
|
return $result['value']['current_id']; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|