Files
clinicpro/migrations/Version20260806132952.php
hamedandClaude Opus 5 9af763bfbe feat(resource): let a resource type declare the fields recorded against it
What an operator writes down after treating an area is decided by the device,
not by the service: a laser has energy, pulse and shot count, an RF unit has
something else. So the field list lives on the resource type, and adding a new
kind of device becomes a settings change rather than a migration.

One validator covers both directions — the schema when a manager saves it and
the values when an operator submits them. Splitting them would let a schema be
stored that no value can ever satisfy.

A value whose key is not in the schema is rejected rather than stored: silently
keeping it means the operator believes they recorded something that will never
be shown back to them. Option matching compares as strings so "18" and 18 are
one option, not two.

The migration seeds the laser type's three fields onto existing rows that have
none, so clinics already running laser devices do not start from an empty form.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-06 17:05:48 +03:30

40 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260806132952 extends AbstractMigration
{
public function getDescription(): string
{
return 'Let a resource type declare the fields an operator records for it';
}
public function up(Schema $schema): void
{
// NULL یعنی این نوع منبع فرم ثبت درمان ندارد — حالت اتاق و پرسنل.
$this->addSql('ALTER TABLE resource_types ADD field_schema JSON DEFAULT NULL');
$this->addSql(
'UPDATE resource_types SET field_schema = :schema WHERE code = :code AND field_schema IS NULL',
[
'code' => 'laser',
'schema' => json_encode([
['key' => 'energy', 'label' => 'انرژی', 'type' => 'select', 'required' => true, 'sort_order' => 0, 'options' => [7, 8, 9, 10, 12, 14, 16, 18]],
['key' => 'pulse', 'label' => 'پالس', 'type' => 'select', 'required' => true, 'sort_order' => 1, 'options' => [3, 5, 10, 20]],
['key' => 'shots', 'label' => 'شات', 'type' => 'number', 'required' => true, 'sort_order' => 2],
], JSON_UNESCAPED_UNICODE),
],
);
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE resource_types DROP field_schema');
}
}