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 * @var Collection
*/ */
#[ORM\OneToMany(targetEntity: "App\Entity\Run", mappedBy: "job")] #[ORM\OneToMany(targetEntity: "App\Entity\Run", mappedBy: "job", cascade: ["remove"])]
private Collection $runs; private Collection $runs;
public function __construct() public function __construct()

View File

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