|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace App\Repository; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
use App\Entity\Base\AbstractDBElement; |
|
25
|
|
|
use Doctrine\ORM\EntityRepository; |
|
26
|
|
|
|
|
27
|
|
|
class DBElementRepository extends EntityRepository |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Changes the ID of the given element to a new value. |
|
31
|
|
|
* You should only use it to undelete former existing elements, everything else is most likely a bad idea! |
|
32
|
|
|
* @param AbstractDBElement $element The element whose ID should be changed |
|
33
|
|
|
* @param int $new_id The new ID |
|
34
|
|
|
*/ |
|
35
|
|
|
public function changeID(AbstractDBElement $element, int $new_id): void |
|
36
|
|
|
{ |
|
37
|
|
|
$qb = $this->createQueryBuilder('element'); |
|
38
|
|
|
$q = $qb->update(get_class($element), 'element') |
|
39
|
|
|
->set('element.id', $new_id) |
|
40
|
|
|
->where('element.id = ?1') |
|
41
|
|
|
->setParameter(1, $element->getID()) |
|
42
|
|
|
->getQuery(); |
|
43
|
|
|
|
|
44
|
|
|
$p = $q->execute(); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$this->setField($element, 'id', $new_id); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function setField(AbstractDBElement $element, string $field, $new_value) |
|
50
|
|
|
{ |
|
51
|
|
|
$reflection = new \ReflectionClass(get_class($element)); |
|
52
|
|
|
$property = $reflection->getProperty($field); |
|
53
|
|
|
$property->setAccessible(true); |
|
54
|
|
|
$property->setValue($element, $new_value); |
|
55
|
|
|
} |
|
56
|
|
|
} |