ENHANCEMENT: Deleting with doctrine

This commit is contained in:
Jeroen De Meerleer 2022-05-18 11:12:35 +02:00
parent 2fa127205c
commit fee48b7a99
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
2 changed files with 6 additions and 4 deletions

View File

@ -57,7 +57,7 @@ class Job
/**
* @var Collection
*/
#[ORM\OneToMany(targetEntity: "App\Entity\Run", mappedBy: "job")]
#[ORM\OneToMany(targetEntity: "App\Entity\Run", mappedBy: "job", cascade: ["remove"])]
private Collection $runs;
public function __construct()

View File

@ -9,7 +9,6 @@ use App\Entity\Run;
use App\Service\Secret;
use DateTime;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use phpseclib3\Crypt\PublicKeyLoader;
@ -673,8 +672,11 @@ class JobRepository extends EntityRepository
public function deleteJob(int $id)
{
$this->getEntityManager()->getConnection()->prepare("DELETE FROM job WHERE id = :id")->executeStatement([':id' => $id]);
$this->getEntityManager()->getConnection()->prepare("DELETE FROM run WHERE job_id = :id")->executeStatement([':id' => $id]);
$em = $this->getEntityManager();
$job = $this->find($id);
$em->remove($job);
$em->flush();
return ['success' => true, 'message' => 'Cronjob succesfully deleted'];
}