2020-03-01 19:46:48 +01:00
|
|
|
<?php
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-03-15 13:56:31 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-03-01 19:46:48 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-03-01 19:46:48 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repository;
|
|
|
|
|
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
2022-08-14 19:32:53 +02:00
|
|
|
use ReflectionClass;
|
2020-03-01 19:46:48 +01:00
|
|
|
|
|
|
|
class DBElementRepository extends EntityRepository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Changes the ID of the given element to a new value.
|
|
|
|
* You should only use it to undelete former existing elements, everything else is most likely a bad idea!
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
|
|
|
* @param AbstractDBElement $element The element whose ID should be changed
|
|
|
|
* @param int $new_id The new ID
|
2020-03-01 19:46:48 +01:00
|
|
|
*/
|
|
|
|
public function changeID(AbstractDBElement $element, int $new_id): void
|
|
|
|
{
|
|
|
|
$qb = $this->createQueryBuilder('element');
|
|
|
|
$q = $qb->update(get_class($element), 'element')
|
|
|
|
->set('element.id', $new_id)
|
|
|
|
->where('element.id = ?1')
|
|
|
|
->setParameter(1, $element->getID())
|
|
|
|
->getQuery();
|
|
|
|
|
2020-03-29 22:37:27 +02:00
|
|
|
//Do the renaming
|
|
|
|
$q->execute();
|
2020-03-01 19:46:48 +01:00
|
|
|
|
|
|
|
$this->setField($element, 'id', $new_id);
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:00:29 +02:00
|
|
|
/**
|
|
|
|
* Find all elements that match a list of IDs.
|
2020-05-10 21:39:31 +02:00
|
|
|
*
|
2020-04-21 00:00:29 +02:00
|
|
|
* @return AbstractDBElement[]
|
|
|
|
*/
|
|
|
|
public function getElementsFromIDArray(array $ids): array
|
|
|
|
{
|
|
|
|
$qb = $this->createQueryBuilder('element');
|
|
|
|
$q = $qb->select('element')
|
|
|
|
->where('element.id IN (?1)')
|
|
|
|
->setParameter(1, $ids)
|
|
|
|
->getQuery();
|
|
|
|
|
|
|
|
return $q->getResult();
|
|
|
|
}
|
|
|
|
|
2020-03-29 23:13:25 +02:00
|
|
|
protected function setField(AbstractDBElement $element, string $field, int $new_value): void
|
2020-03-01 19:46:48 +01:00
|
|
|
{
|
2022-08-14 19:32:53 +02:00
|
|
|
$reflection = new ReflectionClass(get_class($element));
|
2020-03-01 19:46:48 +01:00
|
|
|
$property = $reflection->getProperty($field);
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($element, $new_value);
|
|
|
|
}
|
2020-03-15 13:56:31 +01:00
|
|
|
}
|