Repository 'machine_cluster' utilise entièrement ORM

This commit is contained in:
xdrm-brackets 2016-07-23 17:40:54 +02:00
parent 164ad82f33
commit 9c9ddc1ca9
1 changed files with 123 additions and 155 deletions

View File

@ -23,23 +23,19 @@
public static function create($id_warehouse, $name){
/* [1] Verification de l'unicite de name
=========================================================*/
$check_unique = ( self::getByName($id_warehouse, $name) === false );
$inserted = Table::get('machine_cluster')
->insert([
'id_machine_cluster' => Rows::DEFAULT,
'id_warehouse' => $id_warehouse,
'name' => $name
]);
// Si un groupe a deja ce nom, on renvoie une erreur
if( !$check_unique )
// Si erreur (car name doit être unique)
if( !$inserted )
return false;
/* [2] Creation du groupe
=========================================================*/
$insert_cluster = Database::getPDO()->prepare("INSERT INTO machine_cluster(id_machine_cluster, id_warehouse, name)
VALUES(DEFAULT, :id_warehouse, :name)");
$insert_cluster->execute([
':id_warehouse' => $id_warehouse,
':name' => $name
]);
/* [3] On retourne l'id_group ou FALSE si erreur
/* [2] On retourne l'id_group ou FALSE si erreur
=========================================================*/
$check_group = self::getByName($id_warehouse, $name);
@ -68,56 +64,66 @@
*
*/
public static function link($id_warehouse, $id_machine_cluster, $id_machine){
/* [1] Verification de l'existence du groupe
/* [1] On vérifie que tout existe
=========================================================*/
$group_check = self::getById($id_warehouse, $id_machine_cluster);
/* (1) On récupère le groupe */
$clusterR = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster)
->unique()
->select('id_machine_cluster');
$cluster = $clusterR->fetch();
// Si le groupe n'existe pas, on retourne ue erreur
if( $group_check === false )
// Si le groupe n'existe pas
if( $cluster === false )
return ManagerError::NoMatchFound;
/* (2) On récupère l'utilisateur */
$machineR = Table::get('machine')
->whereIdWarehouse($id_warehouse)
->whereId($id_machine)
->unique()
->select('id_machine');
$machine = $machineR->fetch();
/* [2] Verification de l'existente de la machine
=========================================================*/
$machine_checkReq = new Repo('machine/getById', [$id_warehouse, $id_machine]);
$machine_check = $machine_checkReq->answer();
// Si aucune machine existante, on retourne une erreur
if( $machine_check === false )
// Si l'utilisateur n'existe pas
if( $machine === false )
return ManagerError::NoMatchFound;
/* (3) On vérifie qu'il n'y a pas déja d'appartenance */
$cluster_mergeR = Table::get('machine_cluster_merge')
->select('id_machine_cluster_merge')
->join('id_machine_cluster', $clusterR)
->join('id_machine', $machineR)
->unique();
$cluster_merge_exists = $cluster_mergeR->fetch();
/* [3] Verification que l'existente n'existe pas deja
=========================================================*/
$already_machine_cluster_merge = self::getMerge($id_warehouse, $id_machine_cluster, $id_machine);
// Si l'association existe deja, on ne la cree donc pas
if( $already_machine_cluster_merge !== false )
// Si la relation existe déja, on ne fait rien
if( $cluster_merge_exists !== false )
return ManagerError::Success;
/* [4] On cree l'association
=========================================================*/
$link_entity = Database::getPDO()->prepare("INSERT INTO machine_cluster_merge(id_machine_cluster_merge, id_machine_cluster, id_machine)
VALUES(DEFAULT, :id_machine_cluster, :id_machine)");
$link_entity->execute([
':id_machine_cluster' => $id_machine_cluster,
':id_machine' => $id_machine
]);
/* [5] On retourne l'id_machine_cluster_merge ou FALSE si erreur
/* [2] On crée la relation
=========================================================*/
$check_machine_cluster_merge = self::getMerge($id_warehouse, $id_machine_cluster, $id_machine);
/* (1) On insère la relation */
$inserted = Table::get('machine_cluster_merge')
->insert([
'id_machine_cluster_merge' => Rows::DEFAULT,
'id_machine_cluster' => $id_machine_cluster,
'id_machine' => $id_machine
]);
// Si n'existe pas on retourne FALSE
if( !is_array($check_machine_cluster_merge) )
/* (2) Si erreur, on la retourne */
if( !$inserted )
return ManagerError::RepoError;
// Sinon, on retourne son id
return ManagerError::Success;
/* (3) On vérifie quand même que l'entrée existe */
$cluster_merge_inserted = $cluster_mergeR->fetch();
/* (4) Sinon, on renvoie que tout s'est bien passé */
return is_array($cluster_merge_inserted) ? ManagerError::Success : ManagerError::RepoError;
}
@ -150,12 +156,13 @@
/* [2] On supprime le lien
=========================================================*/
$delete_machine_cluster_merge = Database::getPDO()->prepare("DELETE FROM machine_cluster_merge
WHERE id_machine_cluster_merge = :uid");
$delete_machine_cluster_merge->execute([
':uid' => $get_merge_id['id_machine_cluster_merge']
]);
$deleted = Table::get('machine_cluster_merge')
->whereId($get_merge_id['id_machine_cluster_merge'])
->delete();
// Si erreur
if( !$deleted )
return ManagerError::RepoError;
/* [2] On verifie que l'association n'existe plus
=========================================================*/
@ -183,30 +190,19 @@
*
*/
public static function getMerge($id_warehouse, $id_machine_cluster, $id_machine){
// On ecrit la requete
$request = Database::getPDO()->prepare("SELECT cm.*
FROM machine_cluster_merge as cm, machine_cluster as c
WHERE cm.id_machine_cluster = c.id_machine_cluster
AND c.id_warehouse = :id_warehouse
AND cm.id_machine_cluster = :id_machine_cluster
AND cm.id_machine = :id_machine");
/* [1] On rédige/exécute la requête
=========================================================*/
$cluster_merge = Table::get('machine_cluster_merge')
->whereIdWarehouse($id_warehouse)
->whereIdUserCluster($id_machine_cluster)
->whereIdUser($id_machine)
->select('*')
->unique()
->fetch();
// On execute la requete
$request->execute([
':id_warehouse' => $id_warehouse,
':id_machine_cluster' => $id_machine_cluster,
':id_machine' => $id_machine
]);
// On recupere 1 seule relation
$answer = $request->fetch();
// Gestion d'erreur -> aucun resultat
if( $answer === false )
return false;
// On retourne en supprimant les indices numeriques
return Database::delNumeric( $answer, true );
/* [2] On retourne le résultat (FALSE si rien trouvé)
=========================================================*/
return $cluster_merge;
}
@ -224,19 +220,17 @@
*
*/
public static function getAll($id_warehouse){
// On ecrit la requete
$request = Database::getPDO()->prepare("SELECT c.id_machine_cluster, c.name
FROM machine_cluster as c
WHERE c.id_warehouse = :id_warehouse
ORDER BY c.name ASC");
/* [1] On rédige/exécute la requête
=========================================================*/
$machine_clusters = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->select('*');
// On execute la requete
$request->execute([
':id_warehouse' => $id_warehouse
]);
//TODO: ORDER BY c.name ASC
// On retourne en supprimant les indices numeriques
return Database::delNumeric( $request->fetchAll() );
/* [2] On retourne le résultat
=========================================================*/
return $machine_clusters->fetch();
}
@ -255,26 +249,17 @@
*
*/
public static function getById($id_warehouse, $id){
// On ecrit la requete
$request = Database::getPDO()->prepare("SELECT c.*
FROM machine_cluster as c
WHERE c.id_warehouse = :id_warehouse
AND c.id_machine_cluster = :id");
/* [1] On rédige/exécute la requête
=========================================================*/
$machine_cluster = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->whereId($id)
->select('*')
->unique();
// On execute la requete
$request->execute([
':id_warehouse' => $id_warehouse,
':id' => $id
]);
$fetch = $request->fetch();
// Si aucun résultat
if( $fetch == false )
return false;
// On retourne en supprimant les indices numeriques
return Database::delNumeric( $fetch, true );
/* [2] On retourne le résultat
=========================================================*/
return $machine_cluster->fetch();
}
@ -293,26 +278,17 @@
*
*/
public static function getByName($id_warehouse, $name){
// On ecrit la requete
$request = Database::getPDO()->prepare("SELECT c.*
FROM machine_cluster as c
WHERE c.id_warehouse = :id_warehouse
AND c.name = :name");
/* [1] On rédige/exécute la requête
=========================================================*/
$machine_cluster = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->whereName($name)
->select('*')
->unique();
// On execute la requete
$request->execute([
':id_warehouse' => $id_warehouse,
':name' => $name
]);
$fetch = $request->fetch();
// Si aucun résultat
if( $fetch == false )
return false;
// On retourne en supprimant les indices numeriques
return Database::delNumeric( $fetch, true );
/* [2] On retourne le résultat
=========================================================*/
return $machine_cluster->fetch();
}
@ -334,22 +310,20 @@
public static function getMembers($id_warehouse, $id_machine_cluster){
/* [1] On recupere la liste des associations
=========================================================*/
$get_machines = Database::getPDO()->prepare("SELECT m.id_machine, m.name
FROM machine_cluster_merge as cm, machine_cluster as c, machine as m
WHERE cm.id_machine_cluster = c.id_machine_cluster
AND c.id_machine_cluster = :id_machine_cluster
AND c.id_warehouse = :id_warehouse
AND cm.id_machine = m.id_machine
ORDER BY m.name");
$get_machines->execute([
':id_warehouse' => $id_warehouse,
':id_machine_cluster' => $id_machine_cluster
]);
$machine_cluster = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster);
$machine = Table::get('machine')->select('*');
$machine_cluster_merge = Table::get('machine_cluster_merge')
->join('id_machine_cluster', $machine_cluster)
->join('id_machine', $machine);
/* [2] On verifie que le groupe n'existe plus
=========================================================*/
return Database::delNumeric( $get_machines->fetchAll() );;
return $machine_cluster_merge->fetch();
}
@ -392,17 +366,15 @@
*
*/
public static function edit($id_warehouse, $id_machine_cluster, $name){
/* [1] On rédige la requête
/* [1] On redige/execute la requete
=========================================================*/
$edit_group = Database::getPDO()->prepare("UPDATE machine_cluster
SET name = :name
WHERE id_machine_cluster = :id_machine_cluster");
$edited = $edit_group->execute([
':name' => $name,
':id_machine_cluster' => $id_machine_cluster
]);
$edited = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster)
->edit([ 'name' => $name ]);
/* [2] On retourne le résultat
=========================================================*/
return $edited;
}
@ -422,18 +394,14 @@
public static function delete($id_warehouse, $id_machine_cluster){
/* [1] On redige/execute la requete
=========================================================*/
$delete_group = Database::getPDO()->prepare("DELETE FROM machine_cluster
WHERE id_warehouse = :id_warehouse
AND id_machine_cluster = :id_machine_cluster");
$delete_group->execute([
':id_warehouse' => $id_warehouse,
':id_machine_cluster' => $id_machine_cluster
]);
$deleted = Table::get('machine_cluster')
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster)
->delete();
/* [2] On verifie que le groupe n'existe plus
/* [2] On retourne le résultat
=========================================================*/
return ( self::getById($id_warehouse, $id_machine_cluster) === false );
return $deleted;
}