cycle/entity-behavior-uuid
Cycle ORM behavior that adds first-class UUID columns using ramsey/uuid. Annotate entities with Uuid4 and map fields as type "uuid" (including primary keys) for automatic UUID handling in Cycle ORM models.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require cycle/entity-behavior-uuid
Add the behavior to your entity class:
use Cycle\Annotated\Annotation\Behavior;
use Cycle\EntityBehavior\UUID;
#[Behavior(UUID::class)]
class User {}
First Use Case
$user = new User();
$user->id; // Returns a UUID string (e.g., "123e4567-e89b-12d3-a456-426614174000")
$user = $orm->getRepository(User::class)->find('123e4567-e89b-12d3-a456-426614174000');
Where to Look First
UUID (for configuration options).UUID Generation
Ramsey\Uuid\Uuid::uuid4() (auto-injected via DI).$behavior = new UUID(new CustomUuidGenerator());
Database Schema
uuid-ossp or uuid column type (PostgreSQL/MySQL):
-- PostgreSQL
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
...
);
36 (standard UUIDv4).Schema Integration (New in 1.2.0) Define UUID fields directly in your ORM schema for declarative configuration:
use Cycle\Annotated\Annotation\Entity;
use Cycle\ORM\Schema;
#[Entity]
class User
{
#[Schema\Column(type: 'uuid', primary: true)]
public string $id;
}
This automatically integrates with the UUID behavior, eliminating the need for manual annotations.
Integration with Cycle ORM
$user = $repository->findBy(['email' => 'test@example.com']);
where clauses:
$query = $orm->getRepository(User::class)
->where('id', '=', '123e4567-e89b-12d3-a456-426614174000');
Migrations
use Cycle\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up(): void
{
$this->table('users')->addColumn('id', 'uuid', ['primary' => true]);
}
}
$this->table('users')->addColumn('id', 'uuid', [
'primary' => true,
'default' => fn() => Uuid::uuid4()->toString(),
'generated' => true, // Explicitly mark as generated
]);
API Responses
Ramsey\Uuid\Uuid objects:
$user->id->toString(); // Force string output
Database Compatibility
uuid() extension or CHAR(36) column type.
ALTER TABLE users MODIFY id CHAR(36) NOT NULL;
TEXT with length 36 (no native UUID support).UUID Collisions
Performance
CHAR(36)) is faster than binary in some databases.Schema Integration (New in 1.2.0)
#[Behavior(UUID::class)]), ensure your schema definitions align with the new declarative approach.Serialization
Ramsey\Uuid\Uuid objects by default. Use accessors:
public function getIdAttribute(): string
{
return $this->id->toString();
}
Missing UUIDs
$entity->getBehavior(UUID::class); // Should return the behavior instance
DI Container Issues
Ramsey\Uuid\UuidFactory is bound in your DI container:
$container->bind(UuidFactory::class, fn() => Uuid::getFactory());
Schema Conflicts
Logging
$orm->getConfig()->debug = true;
Custom UUID Formats
#[Schema\Column(type: 'uuid', primary: true)]
public string $id;
// In your entity constructor or behavior setup:
$behavior = new UUID(new UuidFactory(), Uuid::UUID1);
Hybrid IDs
#[Schema\Column(type: 'uuid', primary: true)]
public string $id;
#[Schema\Column(type: 'integer', autoIncrement: true)]
public int $sortId;
Testing
$mockFactory = $this->createMock(UuidFactory::class);
$mockFactory->method('uuid4')->willReturn(Uuid::fromString('test-uuid'));
$behavior = new UUID($mockFactory);
Extensions
use Illuminate\Validation\Rule;
$request->validate([
'id' => ['required', Rule::uuid()],
]);
Schema-Based Workflows
use Cycle\ORM\Schema;
#[Schema\Column(type: 'uuid', primary: true)]
public string $id;
// Reuse in other entities:
#[Schema\Column(type: 'uuid')]
public string $externalId;
Migration Helpers
Schema builder for UUID columns with generated defaults:
$this->table('users')->addColumn('id', 'uuid', [
'primary' => true,
'default' => fn() => Uuid::uuid4()->toString(),
'generated' => true,
]);
NO_UPDATE_NEEDED would not apply here due to the new schema integration feature. The assessment has been fully updated to reflect the changes in **1.2.0**.
How can I help you explore Laravel packages today?