Repository 'machine_cluster' utilise entièrement ORM
This commit is contained in:
parent
164ad82f33
commit
9c9ddc1ca9
|
@ -23,23 +23,19 @@
|
||||||
public static function create($id_warehouse, $name){
|
public static function create($id_warehouse, $name){
|
||||||
/* [1] Verification de l'unicite de 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
|
// Si erreur (car name doit être unique)
|
||||||
if( !$check_unique )
|
if( !$inserted )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
||||||
/* [2] Creation du groupe
|
/* [2] On retourne l'id_group ou FALSE si erreur
|
||||||
=========================================================*/
|
|
||||||
$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
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$check_group = self::getByName($id_warehouse, $name);
|
$check_group = self::getByName($id_warehouse, $name);
|
||||||
|
|
||||||
|
@ -68,56 +64,66 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function link($id_warehouse, $id_machine_cluster, $id_machine){
|
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
|
// Si le groupe n'existe pas
|
||||||
if( $group_check === false )
|
if( $cluster === false )
|
||||||
return ManagerError::NoMatchFound;
|
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();
|
||||||
|
|
||||||
|
// Si l'utilisateur n'existe pas
|
||||||
/* [2] Verification de l'existente de la machine
|
if( $machine === false )
|
||||||
=========================================================*/
|
|
||||||
$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 )
|
|
||||||
return ManagerError::NoMatchFound;
|
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
|
// Si la relation existe déja, on ne fait rien
|
||||||
=========================================================*/
|
if( $cluster_merge_exists !== false )
|
||||||
$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 )
|
|
||||||
return ManagerError::Success;
|
return ManagerError::Success;
|
||||||
|
|
||||||
|
|
||||||
/* [4] On cree l'association
|
|
||||||
|
/* [2] On crée la relation
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$link_entity = Database::getPDO()->prepare("INSERT INTO machine_cluster_merge(id_machine_cluster_merge, id_machine_cluster, id_machine)
|
/* (1) On insère la relation */
|
||||||
VALUES(DEFAULT, :id_machine_cluster, :id_machine)");
|
$inserted = Table::get('machine_cluster_merge')
|
||||||
$link_entity->execute([
|
->insert([
|
||||||
':id_machine_cluster' => $id_machine_cluster,
|
'id_machine_cluster_merge' => Rows::DEFAULT,
|
||||||
':id_machine' => $id_machine
|
'id_machine_cluster' => $id_machine_cluster,
|
||||||
|
'id_machine' => $id_machine
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/* [5] On retourne l'id_machine_cluster_merge ou FALSE si erreur
|
|
||||||
=========================================================*/
|
|
||||||
$check_machine_cluster_merge = self::getMerge($id_warehouse, $id_machine_cluster, $id_machine);
|
|
||||||
|
|
||||||
// Si n'existe pas on retourne FALSE
|
/* (2) Si erreur, on la retourne */
|
||||||
if( !is_array($check_machine_cluster_merge) )
|
if( !$inserted )
|
||||||
return ManagerError::RepoError;
|
return ManagerError::RepoError;
|
||||||
|
|
||||||
// Sinon, on retourne son id
|
/* (3) On vérifie quand même que l'entrée existe */
|
||||||
return ManagerError::Success;
|
$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
|
/* [2] On supprime le lien
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$delete_machine_cluster_merge = Database::getPDO()->prepare("DELETE FROM machine_cluster_merge
|
$deleted = Table::get('machine_cluster_merge')
|
||||||
WHERE id_machine_cluster_merge = :uid");
|
->whereId($get_merge_id['id_machine_cluster_merge'])
|
||||||
$delete_machine_cluster_merge->execute([
|
->delete();
|
||||||
':uid' => $get_merge_id['id_machine_cluster_merge']
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
// Si erreur
|
||||||
|
if( !$deleted )
|
||||||
|
return ManagerError::RepoError;
|
||||||
|
|
||||||
/* [2] On verifie que l'association n'existe plus
|
/* [2] On verifie que l'association n'existe plus
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -183,30 +190,19 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function getMerge($id_warehouse, $id_machine_cluster, $id_machine){
|
public static function getMerge($id_warehouse, $id_machine_cluster, $id_machine){
|
||||||
// On ecrit la requete
|
/* [1] On rédige/exécute la requête
|
||||||
$request = Database::getPDO()->prepare("SELECT cm.*
|
=========================================================*/
|
||||||
FROM machine_cluster_merge as cm, machine_cluster as c
|
$cluster_merge = Table::get('machine_cluster_merge')
|
||||||
WHERE cm.id_machine_cluster = c.id_machine_cluster
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND c.id_warehouse = :id_warehouse
|
->whereIdUserCluster($id_machine_cluster)
|
||||||
AND cm.id_machine_cluster = :id_machine_cluster
|
->whereIdUser($id_machine)
|
||||||
AND cm.id_machine = :id_machine");
|
->select('*')
|
||||||
|
->unique()
|
||||||
|
->fetch();
|
||||||
|
|
||||||
// On execute la requete
|
/* [2] On retourne le résultat (FALSE si rien trouvé)
|
||||||
$request->execute([
|
=========================================================*/
|
||||||
':id_warehouse' => $id_warehouse,
|
return $cluster_merge;
|
||||||
':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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -224,19 +220,17 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function getAll($id_warehouse){
|
public static function getAll($id_warehouse){
|
||||||
// On ecrit la requete
|
/* [1] On rédige/exécute la requête
|
||||||
$request = Database::getPDO()->prepare("SELECT c.id_machine_cluster, c.name
|
=========================================================*/
|
||||||
FROM machine_cluster as c
|
$machine_clusters = Table::get('machine_cluster')
|
||||||
WHERE c.id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
ORDER BY c.name ASC");
|
->select('*');
|
||||||
|
|
||||||
// On execute la requete
|
//TODO: ORDER BY c.name ASC
|
||||||
$request->execute([
|
|
||||||
':id_warehouse' => $id_warehouse
|
|
||||||
]);
|
|
||||||
|
|
||||||
// On retourne en supprimant les indices numeriques
|
/* [2] On retourne le résultat
|
||||||
return Database::delNumeric( $request->fetchAll() );
|
=========================================================*/
|
||||||
|
return $machine_clusters->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -255,26 +249,17 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function getById($id_warehouse, $id){
|
public static function getById($id_warehouse, $id){
|
||||||
// On ecrit la requete
|
/* [1] On rédige/exécute la requête
|
||||||
$request = Database::getPDO()->prepare("SELECT c.*
|
=========================================================*/
|
||||||
FROM machine_cluster as c
|
$machine_cluster = Table::get('machine_cluster')
|
||||||
WHERE c.id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND c.id_machine_cluster = :id");
|
->whereId($id)
|
||||||
|
->select('*')
|
||||||
|
->unique();
|
||||||
|
|
||||||
// On execute la requete
|
/* [2] On retourne le résultat
|
||||||
$request->execute([
|
=========================================================*/
|
||||||
':id_warehouse' => $id_warehouse,
|
return $machine_cluster->fetch();
|
||||||
':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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -293,26 +278,17 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function getByName($id_warehouse, $name){
|
public static function getByName($id_warehouse, $name){
|
||||||
// On ecrit la requete
|
/* [1] On rédige/exécute la requête
|
||||||
$request = Database::getPDO()->prepare("SELECT c.*
|
=========================================================*/
|
||||||
FROM machine_cluster as c
|
$machine_cluster = Table::get('machine_cluster')
|
||||||
WHERE c.id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND c.name = :name");
|
->whereName($name)
|
||||||
|
->select('*')
|
||||||
|
->unique();
|
||||||
|
|
||||||
// On execute la requete
|
/* [2] On retourne le résultat
|
||||||
$request->execute([
|
=========================================================*/
|
||||||
':id_warehouse' => $id_warehouse,
|
return $machine_cluster->fetch();
|
||||||
':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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -334,22 +310,20 @@
|
||||||
public static function getMembers($id_warehouse, $id_machine_cluster){
|
public static function getMembers($id_warehouse, $id_machine_cluster){
|
||||||
/* [1] On recupere la liste des associations
|
/* [1] On recupere la liste des associations
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$get_machines = Database::getPDO()->prepare("SELECT m.id_machine, m.name
|
$machine_cluster = Table::get('machine_cluster')
|
||||||
FROM machine_cluster_merge as cm, machine_cluster as c, machine as m
|
->whereIdWarehouse($id_warehouse)
|
||||||
WHERE cm.id_machine_cluster = c.id_machine_cluster
|
->whereId($id_machine_cluster);
|
||||||
AND c.id_machine_cluster = :id_machine_cluster
|
|
||||||
AND c.id_warehouse = :id_warehouse
|
$machine = Table::get('machine')->select('*');
|
||||||
AND cm.id_machine = m.id_machine
|
|
||||||
ORDER BY m.name");
|
$machine_cluster_merge = Table::get('machine_cluster_merge')
|
||||||
$get_machines->execute([
|
->join('id_machine_cluster', $machine_cluster)
|
||||||
':id_warehouse' => $id_warehouse,
|
->join('id_machine', $machine);
|
||||||
':id_machine_cluster' => $id_machine_cluster
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
|
||||||
/* [2] On verifie que le groupe n'existe plus
|
/* [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){
|
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
|
$edited = Table::get('machine_cluster')
|
||||||
SET name = :name
|
->whereIdWarehouse($id_warehouse)
|
||||||
WHERE id_machine_cluster = :id_machine_cluster");
|
->whereId($id_machine_cluster)
|
||||||
$edited = $edit_group->execute([
|
->edit([ 'name' => $name ]);
|
||||||
':name' => $name,
|
|
||||||
':id_machine_cluster' => $id_machine_cluster
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [2] On retourne le résultat
|
||||||
|
=========================================================*/
|
||||||
return $edited;
|
return $edited;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -422,18 +394,14 @@
|
||||||
public static function delete($id_warehouse, $id_machine_cluster){
|
public static function delete($id_warehouse, $id_machine_cluster){
|
||||||
/* [1] On redige/execute la requete
|
/* [1] On redige/execute la requete
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$delete_group = Database::getPDO()->prepare("DELETE FROM machine_cluster
|
$deleted = Table::get('machine_cluster')
|
||||||
WHERE id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND id_machine_cluster = :id_machine_cluster");
|
->whereId($id_machine_cluster)
|
||||||
$delete_group->execute([
|
->delete();
|
||||||
':id_warehouse' => $id_warehouse,
|
|
||||||
':id_machine_cluster' => $id_machine_cluster
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
/* [2] On retourne le résultat
|
||||||
/* [2] On verifie que le groupe n'existe plus
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
return ( self::getById($id_warehouse, $id_machine_cluster) === false );
|
return $deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue