Tipuri de stocare
Atributele de stocare mapează proprietățile PHP pe tipuri de coloane SQL. Se pun pe fiecare proprietate a modelului.
Tipuri disponibile
| Atribut | Tip SQL | Tip PHP |
|---|---|---|
#[Varchar(255)] | VARCHAR(n) | string |
#[Integer] | INT | int |
#[Boolean] | TINYINT(1) | bool |
#[Decimal(10,2)] | DECIMAL(p,s) | BcMath\Number |
#[FloatingPoint] | FLOAT | float |
#[Date] | DATE | DateTimeImmutable |
#[DateTime] | DATETIME | DateTimeImmutable |
#[Timestamp] | TIMESTAMP | DateTimeImmutable |
#[Time] | TIME | DateTimeImmutable |
#[Text] | TEXT | string |
#[JSON] | JSON | array |
#[Binary] | BLOB | string |
#[Enum(MyEnum::class)] | VARCHAR | BackedEnum |
#[PHPSerializable] | LONGTEXT | mixed (serializat) |
#[Other('BIGINT(20)')] | Tip SQL personalizat | Depinde de caz |
Exemplu
use Leto\Database\ORM\Attributes\DataStorage\Varchar;
use Leto\Database\ORM\Attributes\DataStorage\Integer;
use Leto\Database\ORM\Attributes\DataStorage\Decimal;
use Leto\Database\ORM\Attributes\DataStorage\Boolean;
use Leto\Database\ORM\Attributes\DataStorage\DateTime;
use Leto\Database\ORM\Attributes\DataStorage\JSON;
use Leto\Database\ORM\Attributes\DataStorage\Enum;
use BcMath\Number;
class Product extends Model
{
#[Varchar(200)]
public string $name;
#[Decimal(10, 2)]
public Number $price;
#[Boolean]
public bool $active;
#[DateTime]
public \DateTimeImmutable $created_at;
#[JSON]
public array $metadata;
#[Enum(ProductType::class)]
public ProductType $type;
}#[DefaultValue]
Setează o valoare implicită la nivel de aplicație:
#[Boolean]
#[DefaultValue(true)]
public bool $active = true;#[PHPSerializable] și #[Other]
PHPSerializable
Serializare PHP pentru array-uri arbitrare. Util când ai nevoie de flexibilitate dar nu interoghezi pe conținut:
#[PHPSerializable]
public ?array $state = null;Exemplu real: JobModel din Jobs stochează $state ca array serializat.
Other
Pentru tipuri SQL care nu sunt acoperite de atributele standard:
#[Other('BIGINT(20)')]
#[PrimaryKey, AutoIncrement]
public int $id;Auto-detectare tip
Dacă o proprietate nu are un atribut de stocare explicit, ORM-ul încearcă să deducă tipul din tipul PHP declarat:
| Tip PHP | Tip SQL dedus |
|---|---|
int | INT |
string | VARCHAR(255) |
bool | TINYINT(1) |
float | FLOAT |
DateTimeImmutable | DATETIME |
array | JSON |