mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-03 20:49:41 +00:00
- Changed `getUser` method in `AuditTrail` to allow null values by updating the return type to `?User`. - Updated `insert` method in `AuditTrailRepository` to handle nullable users, ensuring the `user_id` parameter is set only when applicable. - Improved code robustness by adding null-check logic for user assignment in the SQL query.
63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Repository;
|
|
|
|
use Chill\MainBundle\Entity\AuditTrail;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\DBAL\Types\Types;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<AuditTrail>
|
|
*/
|
|
class AuditTrailRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, AuditTrail::class);
|
|
}
|
|
|
|
/**
|
|
* Persist an AuditTrail without storing it in the entity manager.
|
|
*/
|
|
public function persistImmediately(AuditTrail $auditTrail): void
|
|
{
|
|
$metadata = $this->getClassMetadata();
|
|
|
|
$sql = $this->getEntityManager()->getConnection()->createQueryBuilder();
|
|
$sql
|
|
->insert($metadata->getTableName())
|
|
->values([
|
|
'id' => ':id',
|
|
'action' => ':action',
|
|
'occurredat' => ':occured_at',
|
|
'user_id' => ':user_id',
|
|
'description' => ':description',
|
|
'targets' => ':targets',
|
|
'metadata' => ':metadata',
|
|
])
|
|
->setParameter('id', $auditTrail->getId())
|
|
->setParameter('action', $auditTrail->getAction())
|
|
->setParameter('occured_at', $auditTrail->getOccurredAt(), Types::DATETIMETZ_IMMUTABLE)
|
|
->setParameter('user_id', null)
|
|
->setParameter('description', $auditTrail->getDescription())
|
|
->setParameter('targets', $auditTrail->getTargets(), Types::JSON)
|
|
->setParameter('metadata', $auditTrail->getMetadata(), Types::JSON);
|
|
|
|
if (null !== $user = $auditTrail->getUser()) {
|
|
$sql->setParameter('user_id', $user->getId());
|
|
}
|
|
|
|
$sql->executeQuery();
|
|
}
|
|
}
|