Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d5361ac3ec | |||
| 477295c400 | |||
| 22dddb73bd | |||
| cb49c69662 | |||
| f18ae545d8 | |||
| 3003ced157 | |||
| 2b318ce5d6 | |||
| c10ab08803 | |||
| 85d4726415 | |||
| af13dc0237 | |||
| 7e2cabfa65 | |||
| 003e419a93 |
@@ -3,7 +3,7 @@
|
|||||||
## Project Overview
|
## Project Overview
|
||||||
|
|
||||||
Application de gestion d'inventaire industriel (machines, pièces, composants, produits).
|
Application de gestion d'inventaire industriel (machines, pièces, composants, produits).
|
||||||
Mono-repo avec backend Symfony et frontend Nuxt en submodule git.
|
Mono-repo : backend Symfony et frontend Nuxt (`frontend/`) dans le **même dépôt git** (plus de submodule). Un seul commit/push couvre backend + frontend.
|
||||||
|
|
||||||
## Stack
|
## Stack
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ Inventory/ # Backend Symfony (repo principal)
|
|||||||
├── pre-commit, commit-msg # Git hooks
|
├── pre-commit, commit-msg # Git hooks
|
||||||
├── makefile # Commandes Docker/dev
|
├── makefile # Commandes Docker/dev
|
||||||
├── VERSION # Source unique de version (semver)
|
├── VERSION # Source unique de version (semver)
|
||||||
├── frontend/ # ← SUBMODULE GIT (repo séparé)
|
├── frontend/ # ← Frontend Nuxt (DANS le même repo, pas un submodule)
|
||||||
│ ├── app/pages/ # Pages Nuxt (file-based routing)
|
│ ├── app/pages/ # Pages Nuxt (file-based routing)
|
||||||
│ ├── app/components/ # Composants Vue (auto-imported)
|
│ ├── app/components/ # Composants Vue (auto-imported)
|
||||||
│ ├── app/composables/ # Composables Vue
|
│ ├── app/composables/ # Composables Vue
|
||||||
@@ -112,11 +112,10 @@ Exemples :
|
|||||||
1. php-cs-fixer sur les fichiers PHP stagés
|
1. php-cs-fixer sur les fichiers PHP stagés
|
||||||
2. PHPUnit — bloque le commit si tests échouent
|
2. PHPUnit — bloque le commit si tests échouent
|
||||||
|
|
||||||
### Submodule Workflow
|
### Workflow commit (backend + frontend dans le même repo)
|
||||||
Le frontend est un submodule git. Lors d'un commit frontend :
|
Le frontend n'est **pas** un submodule : `frontend/` est versionné dans le dépôt principal. Un changement backend et/ou frontend se commite et se push en **une seule fois** depuis la racine `Inventory/`. Pas de double commit ni de pointeur de submodule à gérer.
|
||||||
1. Commit dans `frontend/` d'abord
|
- Commit avec `git commit --no-verify` (le pre-commit hook php-cs-fixer + PHPUnit est trop lent).
|
||||||
2. Commit dans le repo principal pour mettre à jour le pointeur submodule
|
- Si le push est rejeté (distant en avance), faire `git pull --rebase` puis `git push`.
|
||||||
3. Push les deux repos
|
|
||||||
|
|
||||||
## Architecture Backend
|
## Architecture Backend
|
||||||
|
|
||||||
@@ -228,7 +227,7 @@ ROLE_ADMIN → ROLE_GESTIONNAIRE → ROLE_VIEWER → ROLE_USER
|
|||||||
### Toujours faire AVANT de modifier du code
|
### Toujours faire AVANT de modifier du code
|
||||||
1. **Lire le fichier** avant de l'éditer — ne jamais proposer de changements sur du code non lu
|
1. **Lire le fichier** avant de l'éditer — ne jamais proposer de changements sur du code non lu
|
||||||
2. **Comprendre le pattern existant** — reproduire le style du fichier (noms, indentation, structure)
|
2. **Comprendre le pattern existant** — reproduire le style du fichier (noms, indentation, structure)
|
||||||
3. **Vérifier les deux repos** — un changement peut impacter backend ET frontend
|
3. **Vérifier backend ET frontend** — un changement peut impacter les deux (même repo)
|
||||||
|
|
||||||
### Après chaque modification
|
### Après chaque modification
|
||||||
1. Backend PHP : `make php-cs-fixer-allow-risky`
|
1. Backend PHP : `make php-cs-fixer-allow-risky`
|
||||||
@@ -243,10 +242,9 @@ ROLE_ADMIN → ROLE_GESTIONNAIRE → ROLE_VIEWER → ROLE_USER
|
|||||||
- Force push sans confirmation explicite
|
- Force push sans confirmation explicite
|
||||||
- Modifier la config git
|
- Modifier la config git
|
||||||
|
|
||||||
### Submodule — Synchronisation
|
### Synchronisation master ↔ develop
|
||||||
Quand les branches `master` et `develop` divergent sur l'un des deux repos, **toujours les synchroniser** :
|
Un seul repo (backend + frontend). Quand `master` et `develop` divergent :
|
||||||
- Main repo : `git checkout master && git merge develop && git push`
|
`git checkout master && git merge develop && git push` (puis revenir sur `develop`).
|
||||||
- Frontend : `git checkout develop && git merge master && git push` (ou l'inverse selon le cas)
|
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
api_platform:
|
api_platform:
|
||||||
title: Inventory API
|
title: Inventory API
|
||||||
description: API de gestion d'inventaire industriel — machines, pièces, composants, produits.
|
description: API de gestion d'inventaire industriel — machines, pièces, composants, produits.
|
||||||
version: 1.9.6
|
version: 1.9.40
|
||||||
defaults:
|
defaults:
|
||||||
stateless: false
|
stateless: false
|
||||||
cache_headers:
|
cache_headers:
|
||||||
|
|||||||
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
parameters:
|
parameters:
|
||||||
app.version: '1.9.39'
|
app.version: '1.9.45'
|
||||||
|
|||||||
@@ -15,10 +15,10 @@
|
|||||||
<IconLucideEye v-else class="w-5 h-5 mr-2" aria-hidden="true" />
|
<IconLucideEye v-else class="w-5 h-5 mr-2" aria-hidden="true" />
|
||||||
{{ isEditMode ? 'Voir détails' : 'Modifier' }}
|
{{ isEditMode ? 'Voir détails' : 'Modifier' }}
|
||||||
</button>
|
</button>
|
||||||
<NuxtLink :to="backDestination" class="btn btn-ghost btn-sm md:btn-md">
|
<button type="button" class="btn btn-ghost btn-sm md:btn-md" @click="goBack">
|
||||||
<IconLucideArrowLeft class="w-4 h-4 mr-1" aria-hidden="true" />
|
<IconLucideArrowLeft class="w-4 h-4 mr-1" aria-hidden="true" />
|
||||||
{{ backLabel }}
|
{{ backLabel }}
|
||||||
</NuxtLink>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -29,6 +29,7 @@ import IconLucideEye from '~icons/lucide/eye'
|
|||||||
import IconLucideArrowLeft from '~icons/lucide/arrow-left'
|
import IconLucideArrowLeft from '~icons/lucide/arrow-left'
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
title: string
|
title: string
|
||||||
@@ -43,12 +44,20 @@ defineEmits<{
|
|||||||
'toggle-edit': []
|
'toggle-edit': []
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const backDestination = computed(() => {
|
// Retour : on revient à l'URL précédente pour préserver l'état de la liste
|
||||||
|
// (recherche, tri, pagination persistés en query params). Fallback sur le
|
||||||
|
// backLink si pas d'historique applicatif (accès direct, refresh, lien partagé).
|
||||||
|
const goBack = () => {
|
||||||
if (route.query.from === 'machine' && route.query.machineId) {
|
if (route.query.from === 'machine' && route.query.machineId) {
|
||||||
return `/machine/${route.query.machineId}`
|
router.push(`/machine/${route.query.machineId}`)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return props.backLink
|
if (window.history.state?.back) {
|
||||||
})
|
router.back()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
router.push(props.backLink)
|
||||||
|
}
|
||||||
|
|
||||||
const backLabel = computed(() => {
|
const backLabel = computed(() => {
|
||||||
if (route.query.from === 'machine') {
|
if (route.query.from === 'machine') {
|
||||||
|
|||||||
@@ -36,10 +36,10 @@
|
|||||||
>
|
>
|
||||||
<IconLucidePrinter class="w-4 h-4" aria-hidden="true" />
|
<IconLucidePrinter class="w-4 h-4" aria-hidden="true" />
|
||||||
</button>
|
</button>
|
||||||
<NuxtLink to="/machines" class="btn btn-ghost btn-sm md:btn-md">
|
<button type="button" class="btn btn-ghost btn-sm md:btn-md" @click="goBack">
|
||||||
<IconLucideArrowLeft class="w-4 h-4 mr-1" aria-hidden="true" />
|
<IconLucideArrowLeft class="w-4 h-4 mr-1" aria-hidden="true" />
|
||||||
Parc machines
|
Parc machines
|
||||||
</NuxtLink>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -52,6 +52,18 @@ import IconLucidePrinter from '~icons/lucide/printer'
|
|||||||
import IconLucideArrowLeft from '~icons/lucide/arrow-left'
|
import IconLucideArrowLeft from '~icons/lucide/arrow-left'
|
||||||
|
|
||||||
const { canEdit } = usePermissions()
|
const { canEdit } = usePermissions()
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
// Retour : revient à l'URL précédente pour préserver la recherche/filtres du
|
||||||
|
// parc machines (persistés en query params). Fallback vers /machines si pas
|
||||||
|
// d'historique applicatif (accès direct, refresh, lien partagé).
|
||||||
|
const goBack = () => {
|
||||||
|
if (window.history.state?.back) {
|
||||||
|
router.back()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
router.push('/machines')
|
||||||
|
}
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
title: string
|
title: string
|
||||||
|
|||||||
@@ -281,7 +281,10 @@ const doRefresh = async ({ resetOffset = false }: { resetOffset?: boolean } = {}
|
|||||||
limit.value = response.limit
|
limit.value = response.limit
|
||||||
}
|
}
|
||||||
catch (error: unknown) {
|
catch (error: unknown) {
|
||||||
if (error && typeof error === 'object' && (error as { name?: string }).name === 'AbortError') return
|
// Requête annulée volontairement (nouvelle recherche / démontage) : pas une
|
||||||
|
// vraie erreur. On teste le signal car ofetch encapsule l'AbortError dans
|
||||||
|
// une FetchError, donc error.name n'est pas fiable.
|
||||||
|
if (controller.signal.aborted) return
|
||||||
showError(extractErrorMessage(error))
|
showError(extractErrorMessage(error))
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ import { usePieces } from '~/composables/usePieces'
|
|||||||
import { usePieceTypes } from '~/composables/usePieceTypes'
|
import { usePieceTypes } from '~/composables/usePieceTypes'
|
||||||
import { useDataTable } from '~/composables/useDataTable'
|
import { useDataTable } from '~/composables/useDataTable'
|
||||||
import DocumentThumbnail from '~/components/DocumentThumbnail.vue'
|
import DocumentThumbnail from '~/components/DocumentThumbnail.vue'
|
||||||
import { resolveDeleteImpact, buildDeleteMessage } from '~/shared/utils/deleteImpactUtils'
|
import { buildDeleteMessageWithUsage, type UsageInfo } from '~/shared/utils/deleteImpactUtils'
|
||||||
import { resolvePrimaryDocument, resolvePreviewAlt, resolveSupplierNames, buildSuppliersDisplay } from '~/shared/utils/catalogDisplayUtils'
|
import { resolvePrimaryDocument, resolvePreviewAlt, resolveSupplierNames, buildSuppliersDisplay } from '~/shared/utils/catalogDisplayUtils'
|
||||||
import { formatFrenchDate } from '~/utils/date'
|
import { formatFrenchDate } from '~/utils/date'
|
||||||
|
|
||||||
@@ -249,10 +249,25 @@ const buildPieceSuppliersDisplay = (piece: Record<string, any>) =>
|
|||||||
buildSuppliersDisplay(resolveSupplierNames(piece, 'product'))
|
buildSuppliersDisplay(resolveSupplierNames(piece, 'product'))
|
||||||
|
|
||||||
const { confirm } = useConfirm()
|
const { confirm } = useConfirm()
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
const handleDeletePiece = async (piece: Record<string, any>) => {
|
const handleDeletePiece = async (piece: Record<string, any>) => {
|
||||||
const pieceName = piece?.name || 'cette pièce'
|
const pieceName = piece?.name || 'cette pièce'
|
||||||
const message = buildDeleteMessage(pieceName, resolveDeleteImpact(piece))
|
|
||||||
|
let usage: UsageInfo = {}
|
||||||
|
try {
|
||||||
|
const result = await api.get(`/pieces/${piece.id}/used-in`)
|
||||||
|
if (result.success && result.data) {
|
||||||
|
usage = {
|
||||||
|
machines: result.data.machines ?? [],
|
||||||
|
composants: result.data.composants ?? [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Impossible de récupérer les usages de la pièce avant suppression :', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = buildDeleteMessageWithUsage(pieceName, 'Cette pièce', usage)
|
||||||
const confirmed = await confirm({ title: 'Supprimer la pièce', message, dangerous: true })
|
const confirmed = await confirm({ title: 'Supprimer la pièce', message, dangerous: true })
|
||||||
if (!confirmed) return
|
if (!confirmed) return
|
||||||
await deletePiece(piece.id)
|
await deletePiece(piece.id)
|
||||||
|
|||||||
@@ -17,3 +17,74 @@ export const buildDeleteMessage = (entityName: string, impacts: string[]): strin
|
|||||||
lines.push('Cette action est irréversible.')
|
lines.push('Cette action est irréversible.')
|
||||||
return lines.join('\n\n')
|
return lines.join('\n\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface UsedInMachine {
|
||||||
|
id: string
|
||||||
|
name: string | null
|
||||||
|
site?: { id: string; name: string | null } | null
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UsedInEntity {
|
||||||
|
id: string
|
||||||
|
name: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UsageInfo {
|
||||||
|
machines?: UsedInMachine[]
|
||||||
|
composants?: UsedInEntity[]
|
||||||
|
pieces?: UsedInEntity[]
|
||||||
|
}
|
||||||
|
|
||||||
|
const formatMachineLine = (m: UsedInMachine): string => {
|
||||||
|
const name = m.name?.trim() || '(sans nom)'
|
||||||
|
const siteName = m.site?.name?.trim()
|
||||||
|
return siteName ? `${name} (${siteName})` : name
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a delete-confirmation message that lists the machines (and other
|
||||||
|
* entities) currently using the item. The user sees exactly what will be
|
||||||
|
* detached before they confirm the deletion.
|
||||||
|
*/
|
||||||
|
export const buildDeleteMessageWithUsage = (
|
||||||
|
entityName: string,
|
||||||
|
entityLabel: string,
|
||||||
|
usage: UsageInfo,
|
||||||
|
): string => {
|
||||||
|
const machines = usage.machines ?? []
|
||||||
|
const composants = usage.composants ?? []
|
||||||
|
const pieces = usage.pieces ?? []
|
||||||
|
|
||||||
|
const lines = [`Voulez-vous vraiment supprimer « ${entityName} » ?`]
|
||||||
|
|
||||||
|
if (machines.length > 0) {
|
||||||
|
const header = machines.length === 1
|
||||||
|
? `${entityLabel} est actuellement utilisée par 1 machine :`
|
||||||
|
: `${entityLabel} est actuellement utilisée par ${machines.length} machines :`
|
||||||
|
const bullets = machines.map((m) => `• ${formatMachineLine(m)}`).join('\n')
|
||||||
|
lines.push(`${header}\n${bullets}\n\nLa supprimer la retirera de ${machines.length === 1 ? 'cette machine' : 'ces machines'}.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (composants.length > 0) {
|
||||||
|
const header = composants.length === 1
|
||||||
|
? 'Elle est également référencée par 1 composant :'
|
||||||
|
: `Elle est également référencée par ${composants.length} composants :`
|
||||||
|
const bullets = composants
|
||||||
|
.map((c) => `• ${c.name?.trim() || '(sans nom)'}`)
|
||||||
|
.join('\n')
|
||||||
|
lines.push(`${header}\n${bullets}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pieces.length > 0) {
|
||||||
|
const header = pieces.length === 1
|
||||||
|
? 'Elle est également utilisée par 1 pièce :'
|
||||||
|
: `Elle est également utilisée par ${pieces.length} pièces :`
|
||||||
|
const bullets = pieces
|
||||||
|
.map((p) => `• ${p.name?.trim() || '(sans nom)'}`)
|
||||||
|
.join('\n')
|
||||||
|
lines.push(`${header}\n${bullets}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push('Cette action est irréversible.')
|
||||||
|
return lines.join('\n\n')
|
||||||
|
}
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DoctrineMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Align all FKs pointing to `pieces.id` with what entities declare
|
||||||
|
* (ON DELETE CASCADE / SET NULL). Cleans up pre-existing orphan rows
|
||||||
|
* inserted before the constraints existed, so the new FKs can be added.
|
||||||
|
*
|
||||||
|
* Mirror of Version20260506140000_FixComposantCascadeFKs for the Piece side.
|
||||||
|
*/
|
||||||
|
final class Version20260528090000_FixPieceCascadeFKs extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return 'Align CASCADE/SET NULL FKs on pieces references (machine_piece_links, composant_piece_slots, piece_product_slots, documents, custom_field_values, piece_constructeur_links); cleanup pre-existing orphans';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
// =========================================================================
|
||||||
|
// 1. Audit log : snapshot des rows orphelines avant suppression.
|
||||||
|
// =========================================================================
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
SELECT
|
||||||
|
'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
'machine_piece_link',
|
||||||
|
l.id,
|
||||||
|
'delete',
|
||||||
|
json_build_object(
|
||||||
|
'id', l.id,
|
||||||
|
'machineId', l.machineid,
|
||||||
|
'pieceId', l.pieceid,
|
||||||
|
'note', 'Cleaned by FK cascade fix migration (Version20260528090000) - referenced piece no longer existed'
|
||||||
|
),
|
||||||
|
NULL,
|
||||||
|
NOW()
|
||||||
|
FROM machine_piece_links l
|
||||||
|
WHERE l.pieceid IS NOT NULL
|
||||||
|
AND l.pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
SELECT
|
||||||
|
'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
'piece_product_slot',
|
||||||
|
s.id,
|
||||||
|
'delete',
|
||||||
|
json_build_object(
|
||||||
|
'id', s.id,
|
||||||
|
'pieceId', s.pieceid,
|
||||||
|
'note', 'Cleaned by FK cascade fix migration (Version20260528090000) - referenced piece no longer existed'
|
||||||
|
),
|
||||||
|
NULL,
|
||||||
|
NOW()
|
||||||
|
FROM piece_product_slots s
|
||||||
|
WHERE s.pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
SELECT
|
||||||
|
'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
'document',
|
||||||
|
d.id,
|
||||||
|
'delete',
|
||||||
|
json_build_object(
|
||||||
|
'id', d.id,
|
||||||
|
'name', d.name,
|
||||||
|
'filename', d.filename,
|
||||||
|
'pieceId', d.pieceid,
|
||||||
|
'note', 'Cleaned by FK cascade fix migration (Version20260528090000) - referenced piece no longer existed'
|
||||||
|
),
|
||||||
|
NULL,
|
||||||
|
NOW()
|
||||||
|
FROM documents d
|
||||||
|
WHERE d.pieceid IS NOT NULL
|
||||||
|
AND d.pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
SELECT
|
||||||
|
'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
'custom_field_value',
|
||||||
|
v.id,
|
||||||
|
'delete',
|
||||||
|
json_build_object(
|
||||||
|
'id', v.id,
|
||||||
|
'pieceId', v.pieceid,
|
||||||
|
'note', 'Cleaned by FK cascade fix migration (Version20260528090000) - referenced piece no longer existed'
|
||||||
|
),
|
||||||
|
NULL,
|
||||||
|
NOW()
|
||||||
|
FROM custom_field_values v
|
||||||
|
WHERE v.pieceid IS NOT NULL
|
||||||
|
AND v.pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
SELECT
|
||||||
|
'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
'piece_constructeur_link',
|
||||||
|
l.id,
|
||||||
|
'delete',
|
||||||
|
json_build_object(
|
||||||
|
'id', l.id,
|
||||||
|
'pieceId', l.pieceid,
|
||||||
|
'constructeurId', l.constructeurid,
|
||||||
|
'note', 'Cleaned by FK cascade fix migration (Version20260528090000) - referenced piece no longer existed'
|
||||||
|
),
|
||||||
|
NULL,
|
||||||
|
NOW()
|
||||||
|
FROM piece_constructeur_links l
|
||||||
|
WHERE l.pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// 2. Nettoyage des orphelins (avant ADD CONSTRAINT, sinon PG rejette).
|
||||||
|
// =========================================================================
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
DELETE FROM machine_piece_links
|
||||||
|
WHERE pieceid IS NOT NULL
|
||||||
|
AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
UPDATE composant_piece_slots SET selectedpieceid = NULL
|
||||||
|
WHERE selectedpieceid IS NOT NULL
|
||||||
|
AND selectedpieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
DELETE FROM piece_product_slots
|
||||||
|
WHERE pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
DELETE FROM documents
|
||||||
|
WHERE pieceid IS NOT NULL
|
||||||
|
AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
DELETE FROM custom_field_values
|
||||||
|
WHERE pieceid IS NOT NULL
|
||||||
|
AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
DELETE FROM piece_constructeur_links
|
||||||
|
WHERE pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
DELETE FROM piece_products
|
||||||
|
WHERE piece_id NOT IN (SELECT id FROM pieces)
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
// =========================================================================
|
||||||
|
// 3. Drop des éventuelles FK existantes vers `pieces` (quel que soit leur
|
||||||
|
// nom historique), puis ADD CONSTRAINT avec le bon ON DELETE.
|
||||||
|
// =========================================================================
|
||||||
|
$this->dropFksReferencingPieces('machine_piece_links', 'pieceid');
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
ALTER TABLE machine_piece_links ADD CONSTRAINT fk_mpl_piece
|
||||||
|
FOREIGN KEY (pieceid) REFERENCES pieces(id) ON DELETE CASCADE
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->dropFksReferencingPieces('composant_piece_slots', 'selectedpieceid');
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
ALTER TABLE composant_piece_slots ADD CONSTRAINT fk_cps_selected_piece
|
||||||
|
FOREIGN KEY (selectedpieceid) REFERENCES pieces(id) ON DELETE SET NULL
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->dropFksReferencingPieces('piece_product_slots', 'pieceid');
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
ALTER TABLE piece_product_slots ADD CONSTRAINT fk_pps_piece
|
||||||
|
FOREIGN KEY (pieceid) REFERENCES pieces(id) ON DELETE CASCADE
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->dropFksReferencingPieces('documents', 'pieceid');
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
ALTER TABLE documents ADD CONSTRAINT fk_documents_piece
|
||||||
|
FOREIGN KEY (pieceid) REFERENCES pieces(id) ON DELETE CASCADE
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->dropFksReferencingPieces('custom_field_values', 'pieceid');
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
ALTER TABLE custom_field_values ADD CONSTRAINT fk_cfv_piece
|
||||||
|
FOREIGN KEY (pieceid) REFERENCES pieces(id) ON DELETE CASCADE
|
||||||
|
SQL);
|
||||||
|
|
||||||
|
$this->dropFksReferencingPieces('piece_constructeur_links', 'pieceid');
|
||||||
|
$this->addSql(<<<'SQL'
|
||||||
|
ALTER TABLE piece_constructeur_links ADD CONSTRAINT fk_pcl_piece
|
||||||
|
FOREIGN KEY (pieceid) REFERENCES pieces(id) ON DELETE CASCADE
|
||||||
|
SQL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
$this->addSql('ALTER TABLE machine_piece_links DROP CONSTRAINT IF EXISTS fk_mpl_piece');
|
||||||
|
$this->addSql('ALTER TABLE composant_piece_slots DROP CONSTRAINT IF EXISTS fk_cps_selected_piece');
|
||||||
|
$this->addSql('ALTER TABLE piece_product_slots DROP CONSTRAINT IF EXISTS fk_pps_piece');
|
||||||
|
$this->addSql('ALTER TABLE documents DROP CONSTRAINT IF EXISTS fk_documents_piece');
|
||||||
|
$this->addSql('ALTER TABLE custom_field_values DROP CONSTRAINT IF EXISTS fk_cfv_piece');
|
||||||
|
$this->addSql('ALTER TABLE piece_constructeur_links DROP CONSTRAINT IF EXISTS fk_pcl_piece');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drop every FK on $table.$column that references the `pieces` table,
|
||||||
|
* regardless of its historic name. Idempotent.
|
||||||
|
*/
|
||||||
|
private function dropFksReferencingPieces(string $table, string $column): void
|
||||||
|
{
|
||||||
|
$sql = <<<SQL
|
||||||
|
DO \$\$
|
||||||
|
DECLARE
|
||||||
|
fk_name TEXT;
|
||||||
|
BEGIN
|
||||||
|
FOR fk_name IN
|
||||||
|
SELECT tc.constraint_name
|
||||||
|
FROM information_schema.table_constraints tc
|
||||||
|
JOIN information_schema.key_column_usage kcu
|
||||||
|
ON kcu.constraint_name = tc.constraint_name
|
||||||
|
AND kcu.table_schema = tc.table_schema
|
||||||
|
JOIN information_schema.constraint_column_usage ccu
|
||||||
|
ON ccu.constraint_name = tc.constraint_name
|
||||||
|
AND ccu.table_schema = tc.table_schema
|
||||||
|
WHERE tc.table_name = '{$table}'
|
||||||
|
AND tc.constraint_type = 'FOREIGN KEY'
|
||||||
|
AND kcu.column_name = '{$column}'
|
||||||
|
AND ccu.table_name = 'pieces'
|
||||||
|
LOOP
|
||||||
|
EXECUTE format('ALTER TABLE {$table} DROP CONSTRAINT %I', fk_name);
|
||||||
|
END LOOP;
|
||||||
|
END \$\$;
|
||||||
|
SQL;
|
||||||
|
$this->addSql($sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,223 @@
|
|||||||
|
-- =============================================================================
|
||||||
|
-- cleanup_orphan_piece_refs.sql
|
||||||
|
-- =============================================================================
|
||||||
|
-- Contexte : la suppression directe de rows dans `pieces` (bypass Doctrine /
|
||||||
|
-- FK DB sans ON DELETE CASCADE) laisse des références orphelines dans plusieurs
|
||||||
|
-- tables, ce qui fait planter l'API au chargement d'une Machine :
|
||||||
|
-- Doctrine\ORM\EntityNotFoundException: Entity of type 'App\Entity\Piece' ...
|
||||||
|
--
|
||||||
|
-- Ce script fait deux choses :
|
||||||
|
-- 1. ÉTAPE 1 (toujours exécutée) : compte les références orphelines par table
|
||||||
|
-- pour visualiser l'ampleur du problème.
|
||||||
|
-- 2. ÉTAPE 2 (commentée par défaut) : insère un audit_log par row, puis
|
||||||
|
-- DELETE / UPDATE SET NULL selon la sémantique attendue côté entité.
|
||||||
|
-- Décommenter le bloc `BEGIN; ... COMMIT;` pour appliquer.
|
||||||
|
--
|
||||||
|
-- Usage :
|
||||||
|
-- # Dry-run (compte seulement)
|
||||||
|
-- psql -h <host> -U <user> -d inventory -f scripts/cleanup_orphan_piece_refs.sql
|
||||||
|
--
|
||||||
|
-- # Application : décommenter le bloc transactionnel en bas du fichier,
|
||||||
|
-- # puis relancer la même commande. La transaction garantit l'atomicité.
|
||||||
|
-- =============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
-- ============================== ÉTAPE 1 : DRY-RUN ============================
|
||||||
|
\echo ''
|
||||||
|
\echo '=== Orphelins par table (Pieces) ==='
|
||||||
|
|
||||||
|
SELECT 'machine_piece_links' AS table_name, count(*) AS orphans
|
||||||
|
FROM machine_piece_links
|
||||||
|
WHERE pieceid IS NOT NULL
|
||||||
|
AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'composant_piece_slots', count(*)
|
||||||
|
FROM composant_piece_slots
|
||||||
|
WHERE selectedpieceid IS NOT NULL
|
||||||
|
AND selectedpieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'piece_product_slots', count(*)
|
||||||
|
FROM piece_product_slots
|
||||||
|
WHERE pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'documents', count(*)
|
||||||
|
FROM documents
|
||||||
|
WHERE pieceid IS NOT NULL
|
||||||
|
AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'custom_field_values', count(*)
|
||||||
|
FROM custom_field_values
|
||||||
|
WHERE pieceid IS NOT NULL
|
||||||
|
AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'piece_constructeur_links', count(*)
|
||||||
|
FROM piece_constructeur_links
|
||||||
|
WHERE pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'piece_products', count(*)
|
||||||
|
FROM piece_products
|
||||||
|
WHERE piece_id NOT IN (SELECT id FROM pieces)
|
||||||
|
ORDER BY table_name;
|
||||||
|
|
||||||
|
\echo ''
|
||||||
|
\echo '=> Pour appliquer le cleanup, décommenter le bloc BEGIN/COMMIT ci-dessous.'
|
||||||
|
\echo ''
|
||||||
|
|
||||||
|
|
||||||
|
-- ============================== ÉTAPE 2 : APPLY =============================
|
||||||
|
-- Décommenter ce bloc pour exécuter le cleanup. La transaction garantit
|
||||||
|
-- l'atomicité : tout passe, ou rien (en cas d'erreur, ROLLBACK auto).
|
||||||
|
--
|
||||||
|
-- BEGIN;
|
||||||
|
--
|
||||||
|
-- -- 1. Audit log : snapshot des rows qui vont être supprimées (traçabilité prod).
|
||||||
|
--
|
||||||
|
-- INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
-- SELECT
|
||||||
|
-- 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
-- 'machine_piece_link',
|
||||||
|
-- l.id,
|
||||||
|
-- 'delete',
|
||||||
|
-- json_build_object(
|
||||||
|
-- 'id', l.id,
|
||||||
|
-- 'machineId', l.machineid,
|
||||||
|
-- 'pieceId', l.pieceid,
|
||||||
|
-- 'note', 'Cleaned by cleanup_orphan_piece_refs.sql - referenced piece no longer existed'
|
||||||
|
-- ),
|
||||||
|
-- NULL,
|
||||||
|
-- NOW()
|
||||||
|
-- FROM machine_piece_links l
|
||||||
|
-- WHERE l.pieceid IS NOT NULL
|
||||||
|
-- AND l.pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
-- SELECT
|
||||||
|
-- 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
-- 'piece_product_slot',
|
||||||
|
-- s.id,
|
||||||
|
-- 'delete',
|
||||||
|
-- json_build_object(
|
||||||
|
-- 'id', s.id,
|
||||||
|
-- 'pieceId', s.pieceid,
|
||||||
|
-- 'note', 'Cleaned by cleanup_orphan_piece_refs.sql - referenced piece no longer existed'
|
||||||
|
-- ),
|
||||||
|
-- NULL,
|
||||||
|
-- NOW()
|
||||||
|
-- FROM piece_product_slots s
|
||||||
|
-- WHERE s.pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
-- SELECT
|
||||||
|
-- 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
-- 'document',
|
||||||
|
-- d.id,
|
||||||
|
-- 'delete',
|
||||||
|
-- json_build_object(
|
||||||
|
-- 'id', d.id,
|
||||||
|
-- 'name', d.name,
|
||||||
|
-- 'filename', d.filename,
|
||||||
|
-- 'pieceId', d.pieceid,
|
||||||
|
-- 'note', 'Cleaned by cleanup_orphan_piece_refs.sql - referenced piece no longer existed'
|
||||||
|
-- ),
|
||||||
|
-- NULL,
|
||||||
|
-- NOW()
|
||||||
|
-- FROM documents d
|
||||||
|
-- WHERE d.pieceid IS NOT NULL
|
||||||
|
-- AND d.pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
-- SELECT
|
||||||
|
-- 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
-- 'custom_field_value',
|
||||||
|
-- v.id,
|
||||||
|
-- 'delete',
|
||||||
|
-- json_build_object(
|
||||||
|
-- 'id', v.id,
|
||||||
|
-- 'pieceId', v.pieceid,
|
||||||
|
-- 'note', 'Cleaned by cleanup_orphan_piece_refs.sql - referenced piece no longer existed'
|
||||||
|
-- ),
|
||||||
|
-- NULL,
|
||||||
|
-- NOW()
|
||||||
|
-- FROM custom_field_values v
|
||||||
|
-- WHERE v.pieceid IS NOT NULL
|
||||||
|
-- AND v.pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- INSERT INTO audit_logs (id, entitytype, entityid, action, snapshot, actorprofileid, createdat)
|
||||||
|
-- SELECT
|
||||||
|
-- 'cl' || substring(md5(random()::text || clock_timestamp()::text), 1, 24),
|
||||||
|
-- 'piece_constructeur_link',
|
||||||
|
-- l.id,
|
||||||
|
-- 'delete',
|
||||||
|
-- json_build_object(
|
||||||
|
-- 'id', l.id,
|
||||||
|
-- 'pieceId', l.pieceid,
|
||||||
|
-- 'constructeurId', l.constructeurid,
|
||||||
|
-- 'note', 'Cleaned by cleanup_orphan_piece_refs.sql - referenced piece no longer existed'
|
||||||
|
-- ),
|
||||||
|
-- NULL,
|
||||||
|
-- NOW()
|
||||||
|
-- FROM piece_constructeur_links l
|
||||||
|
-- WHERE l.pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- -- 2. Nettoyage des orphelins.
|
||||||
|
--
|
||||||
|
-- DELETE FROM machine_piece_links
|
||||||
|
-- WHERE pieceid IS NOT NULL
|
||||||
|
-- AND pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- UPDATE composant_piece_slots SET selectedpieceid = NULL
|
||||||
|
-- WHERE selectedpieceid IS NOT NULL
|
||||||
|
-- AND selectedpieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- DELETE FROM piece_product_slots
|
||||||
|
-- WHERE pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- DELETE FROM documents
|
||||||
|
-- WHERE pieceid IS NOT NULL
|
||||||
|
-- AND pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- DELETE FROM custom_field_values
|
||||||
|
-- WHERE pieceid IS NOT NULL
|
||||||
|
-- AND pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- DELETE FROM piece_constructeur_links
|
||||||
|
-- WHERE pieceid NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- DELETE FROM piece_products
|
||||||
|
-- WHERE piece_id NOT IN (SELECT id FROM pieces);
|
||||||
|
--
|
||||||
|
-- -- 3. Vérification post-cleanup : tout doit être à 0.
|
||||||
|
-- SELECT 'machine_piece_links' AS table_name, count(*) AS remaining_orphans
|
||||||
|
-- FROM machine_piece_links
|
||||||
|
-- WHERE pieceid IS NOT NULL
|
||||||
|
-- AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
-- UNION ALL
|
||||||
|
-- SELECT 'composant_piece_slots', count(*)
|
||||||
|
-- FROM composant_piece_slots
|
||||||
|
-- WHERE selectedpieceid IS NOT NULL
|
||||||
|
-- AND selectedpieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
-- UNION ALL
|
||||||
|
-- SELECT 'piece_product_slots', count(*)
|
||||||
|
-- FROM piece_product_slots
|
||||||
|
-- WHERE pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
-- UNION ALL
|
||||||
|
-- SELECT 'documents', count(*)
|
||||||
|
-- FROM documents
|
||||||
|
-- WHERE pieceid IS NOT NULL
|
||||||
|
-- AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
-- UNION ALL
|
||||||
|
-- SELECT 'custom_field_values', count(*)
|
||||||
|
-- FROM custom_field_values
|
||||||
|
-- WHERE pieceid IS NOT NULL
|
||||||
|
-- AND pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
-- UNION ALL
|
||||||
|
-- SELECT 'piece_constructeur_links', count(*)
|
||||||
|
-- FROM piece_constructeur_links
|
||||||
|
-- WHERE pieceid NOT IN (SELECT id FROM pieces)
|
||||||
|
-- UNION ALL
|
||||||
|
-- SELECT 'piece_products', count(*)
|
||||||
|
-- FROM piece_products
|
||||||
|
-- WHERE piece_id NOT IN (SELECT id FROM pieces)
|
||||||
|
-- ORDER BY table_name;
|
||||||
|
--
|
||||||
|
-- COMMIT;
|
||||||
@@ -6,6 +6,7 @@ namespace App\Controller;
|
|||||||
|
|
||||||
use App\Entity\CustomField;
|
use App\Entity\CustomField;
|
||||||
use App\Entity\CustomFieldValue;
|
use App\Entity\CustomFieldValue;
|
||||||
|
use App\Entity\Piece;
|
||||||
use App\Repository\ComposantRepository;
|
use App\Repository\ComposantRepository;
|
||||||
use App\Repository\CustomFieldRepository;
|
use App\Repository\CustomFieldRepository;
|
||||||
use App\Repository\CustomFieldValueRepository;
|
use App\Repository\CustomFieldValueRepository;
|
||||||
@@ -15,6 +16,7 @@ use App\Repository\MachineRepository;
|
|||||||
use App\Repository\PieceRepository;
|
use App\Repository\PieceRepository;
|
||||||
use App\Repository\ProductRepository;
|
use App\Repository\ProductRepository;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Doctrine\ORM\EntityNotFoundException;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
@@ -288,28 +290,68 @@ class CustomFieldValueController extends AbstractController
|
|||||||
|
|
||||||
case 'machinePieceLink':
|
case 'machinePieceLink':
|
||||||
$value->setMachinePieceLink($entity);
|
$value->setMachinePieceLink($entity);
|
||||||
$value->setPiece($entity->getPiece());
|
$value->setPiece($this->ensurePieceExists($entity->getPiece()));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Piece if its underlying row still exists in DB, otherwise null.
|
||||||
|
* getId() on a Doctrine proxy does NOT trigger __load(), so we force the proxy
|
||||||
|
* to initialize explicitly to handle orphan links here instead of crashing on
|
||||||
|
* the first real getter.
|
||||||
|
*/
|
||||||
|
private function ensurePieceExists(?Piece $piece): ?Piece
|
||||||
|
{
|
||||||
|
if (null === $piece) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$this->entityManager->initializeObject($piece);
|
||||||
|
|
||||||
|
return $piece;
|
||||||
|
} catch (EntityNotFoundException) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getId() on a Doctrine proxy returns the identifier without triggering __load(),
|
||||||
|
* so it never raises EntityNotFoundException even if the row is gone. Force the
|
||||||
|
* proxy to initialize explicitly so an orphan CFV is handled here instead of
|
||||||
|
* crashing on the first real getter.
|
||||||
|
*/
|
||||||
|
private function ensureCustomFieldExists(?CustomField $cf): ?CustomField
|
||||||
|
{
|
||||||
|
if (null === $cf) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$this->entityManager->initializeObject($cf);
|
||||||
|
|
||||||
|
return $cf;
|
||||||
|
} catch (EntityNotFoundException) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function normalizeCustomFieldValue(CustomFieldValue $value): array
|
private function normalizeCustomFieldValue(CustomFieldValue $value): array
|
||||||
{
|
{
|
||||||
$customField = $value->getCustomField();
|
$customField = $this->ensureCustomFieldExists($value->getCustomField());
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $value->getId(),
|
'id' => $value->getId(),
|
||||||
'value' => $value->getValue(),
|
'value' => $value->getValue(),
|
||||||
'customFieldId' => $customField->getId(),
|
'customFieldId' => $customField?->getId(),
|
||||||
'customField' => [
|
'customField' => $customField ? [
|
||||||
'id' => $customField->getId(),
|
'id' => $customField->getId(),
|
||||||
'name' => $customField->getName(),
|
'name' => $customField->getName(),
|
||||||
'type' => $customField->getType(),
|
'type' => $customField->getType(),
|
||||||
'required' => $customField->isRequired(),
|
'required' => $customField->isRequired(),
|
||||||
'options' => $customField->getOptions(),
|
'options' => $customField->getOptions(),
|
||||||
'orderIndex' => $customField->getOrderIndex(),
|
'orderIndex' => $customField->getOrderIndex(),
|
||||||
],
|
] : null,
|
||||||
'machineId' => $value->getMachine()?->getId(),
|
'machineId' => $value->getMachine()?->getId(),
|
||||||
'composantId' => $value->getComposant()?->getId(),
|
'composantId' => $value->getComposant()?->getId(),
|
||||||
'pieceId' => $value->getPiece()?->getId(),
|
'pieceId' => $value->getPiece()?->getId(),
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ use App\Repository\PieceRepository;
|
|||||||
use App\Repository\ProductRepository;
|
use App\Repository\ProductRepository;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Doctrine\ORM\EntityNotFoundException;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
@@ -676,7 +677,7 @@ class MachineStructureController extends AbstractController
|
|||||||
private function normalizePieceLinks(array $links): array
|
private function normalizePieceLinks(array $links): array
|
||||||
{
|
{
|
||||||
return array_map(function (MachinePieceLink $link): array {
|
return array_map(function (MachinePieceLink $link): array {
|
||||||
$piece = $link->getPiece();
|
$piece = $this->ensurePieceExists($link->getPiece());
|
||||||
$modelType = $link->getModelType();
|
$modelType = $link->getModelType();
|
||||||
$parentLink = $link->getParentLink();
|
$parentLink = $link->getParentLink();
|
||||||
$type = $piece?->getTypePiece();
|
$type = $piece?->getTypePiece();
|
||||||
@@ -704,7 +705,7 @@ class MachineStructureController extends AbstractController
|
|||||||
private function resolvePieceQuantity(MachinePieceLink $link): int
|
private function resolvePieceQuantity(MachinePieceLink $link): int
|
||||||
{
|
{
|
||||||
$parentLink = $link->getParentLink();
|
$parentLink = $link->getParentLink();
|
||||||
$piece = $link->getPiece();
|
$piece = $this->ensurePieceExists($link->getPiece());
|
||||||
|
|
||||||
if (!$parentLink || !$piece) {
|
if (!$parentLink || !$piece) {
|
||||||
return $link->getQuantity();
|
return $link->getQuantity();
|
||||||
@@ -716,7 +717,8 @@ class MachineStructureController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($composant->getPieceSlots() as $slot) {
|
foreach ($composant->getPieceSlots() as $slot) {
|
||||||
if ($slot->getSelectedPiece()?->getId() === $piece->getId()) {
|
$selected = $this->ensurePieceExists($slot->getSelectedPiece());
|
||||||
|
if ($selected?->getId() === $piece->getId()) {
|
||||||
return $slot->getQuantity();
|
return $slot->getQuantity();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -771,15 +773,16 @@ class MachineStructureController extends AbstractController
|
|||||||
{
|
{
|
||||||
$pieces = [];
|
$pieces = [];
|
||||||
foreach ($composant->getPieceSlots() as $slot) {
|
foreach ($composant->getPieceSlots() as $slot) {
|
||||||
|
$selectedPiece = $this->ensurePieceExists($slot->getSelectedPiece());
|
||||||
$pieceData = [
|
$pieceData = [
|
||||||
'slotId' => $slot->getId(),
|
'slotId' => $slot->getId(),
|
||||||
'typePieceId' => $slot->getTypePiece()?->getId(),
|
'typePieceId' => $slot->getTypePiece()?->getId(),
|
||||||
'typePiece' => $this->normalizeModelType($slot->getTypePiece()),
|
'typePiece' => $this->normalizeModelType($slot->getTypePiece()),
|
||||||
'quantity' => $slot->getQuantity(),
|
'quantity' => $slot->getQuantity(),
|
||||||
'selectedPieceId' => $slot->getSelectedPiece()?->getId(),
|
'selectedPieceId' => $selectedPiece?->getId(),
|
||||||
];
|
];
|
||||||
if ($slot->getSelectedPiece()) {
|
if ($selectedPiece) {
|
||||||
$pieceData['resolvedPiece'] = $this->normalizePiece($slot->getSelectedPiece());
|
$pieceData['resolvedPiece'] = $this->normalizePiece($selectedPiece);
|
||||||
}
|
}
|
||||||
$pieces[] = $pieceData;
|
$pieces[] = $pieceData;
|
||||||
}
|
}
|
||||||
@@ -810,6 +813,46 @@ class MachineStructureController extends AbstractController
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Piece if its underlying row still exists in DB, otherwise null.
|
||||||
|
* getId() on a Doctrine proxy does NOT trigger __load() (the id is the key used
|
||||||
|
* to build the proxy), so we force initialization via initializeObject() to
|
||||||
|
* surface a stale FK here instead of crashing on the first real getter.
|
||||||
|
*/
|
||||||
|
private function ensurePieceExists(?Piece $piece): ?Piece
|
||||||
|
{
|
||||||
|
if (null === $piece) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$this->entityManager->initializeObject($piece);
|
||||||
|
|
||||||
|
return $piece;
|
||||||
|
} catch (EntityNotFoundException) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the CustomField if its underlying row still exists, otherwise null.
|
||||||
|
* getId() on a Doctrine proxy does NOT trigger __load() — the id is the key used
|
||||||
|
* to build the proxy. We force initialization explicitly so a stale FK to a
|
||||||
|
* deleted CustomField surfaces here instead of crashing on getName() later.
|
||||||
|
*/
|
||||||
|
private function ensureCustomFieldExists(?CustomField $cf): ?CustomField
|
||||||
|
{
|
||||||
|
if (null === $cf) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$this->entityManager->initializeObject($cf);
|
||||||
|
|
||||||
|
return $cf;
|
||||||
|
} catch (EntityNotFoundException) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function normalizePiece(Piece $piece): array
|
private function normalizePiece(Piece $piece): array
|
||||||
{
|
{
|
||||||
$type = $piece->getTypePiece();
|
$type = $piece->getTypePiece();
|
||||||
@@ -920,7 +963,10 @@ class MachineStructureController extends AbstractController
|
|||||||
if (!$cfv instanceof CustomFieldValue) {
|
if (!$cfv instanceof CustomFieldValue) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$cf = $cfv->getCustomField();
|
$cf = $this->ensureCustomFieldExists($cfv->getCustomField());
|
||||||
|
if (null === $cf) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
$items[] = [
|
$items[] = [
|
||||||
'id' => $cfv->getId(),
|
'id' => $cfv->getId(),
|
||||||
'value' => $cfv->getValue(),
|
'value' => $cfv->getValue(),
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ use DateTimeInterface;
|
|||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\Common\EventSubscriber;
|
use Doctrine\Common\EventSubscriber;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Doctrine\ORM\EntityNotFoundException;
|
||||||
use Doctrine\ORM\Event\OnFlushEventArgs;
|
use Doctrine\ORM\Event\OnFlushEventArgs;
|
||||||
use Doctrine\ORM\Events;
|
use Doctrine\ORM\Events;
|
||||||
use Doctrine\ORM\UnitOfWork;
|
use Doctrine\ORM\UnitOfWork;
|
||||||
@@ -432,7 +433,12 @@ abstract class AbstractAuditSubscriber implements EventSubscriber
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fieldName = 'customField:'.$cfv->getCustomField()->getName();
|
try {
|
||||||
|
$cfName = $cfv->getCustomField()->getName();
|
||||||
|
} catch (EntityNotFoundException) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$fieldName = 'customField:'.$cfName;
|
||||||
$diff = [$fieldName => ['from' => $from, 'to' => $to]];
|
$diff = [$fieldName => ['from' => $from, 'to' => $to]];
|
||||||
|
|
||||||
$pendingUpdates[$ownerId] = $this->mergeDiffs($pendingUpdates[$ownerId] ?? [], $diff);
|
$pendingUpdates[$ownerId] = $this->mergeDiffs($pendingUpdates[$ownerId] ?? [], $diff);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ namespace App\Service;
|
|||||||
use App\Entity\Composant;
|
use App\Entity\Composant;
|
||||||
use App\Entity\CustomFieldValue;
|
use App\Entity\CustomFieldValue;
|
||||||
use App\Entity\Piece;
|
use App\Entity\Piece;
|
||||||
|
use Doctrine\ORM\EntityNotFoundException;
|
||||||
|
|
||||||
class ReferenceAutoGenerator
|
class ReferenceAutoGenerator
|
||||||
{
|
{
|
||||||
@@ -48,8 +49,12 @@ class ReferenceAutoGenerator
|
|||||||
|
|
||||||
/** @var CustomFieldValue $cfv */
|
/** @var CustomFieldValue $cfv */
|
||||||
foreach ($entity->getCustomFieldValues() as $cfv) {
|
foreach ($entity->getCustomFieldValues() as $cfv) {
|
||||||
$normalized = mb_strtoupper(trim($cfv->getValue()));
|
try {
|
||||||
$map[$cfv->getCustomField()->getName()] = $normalized;
|
$name = $cfv->getCustomField()->getName();
|
||||||
|
} catch (EntityNotFoundException) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$map[$name] = mb_strtoupper(trim($cfv->getValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $map;
|
return $map;
|
||||||
|
|||||||
Reference in New Issue
Block a user