PHP code example of inwebo / doctrine-event-sourcing

1. Go to this page and download the library: Download inwebo/doctrine-event-sourcing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inwebo / doctrine-event-sourcing example snippets



// src/Entity/Product.php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Inwebo\DoctrineEventSourcing\Listener\StoreListener; // Default listener
use Inwebo\DoctrineEventSourcing\Mapping\AggregateRoot;
use Inwebo\DoctrineEventSourcing\Mapping\AggregateSource;
use Inwebo\DoctrineEventSourcing\Model\Interface\HasStatesInterface;
use Inwebo\DoctrineEventSourcing\Model\Interface\StateInterface;

#[AggregateRoot(stateClass: ProductState::class, subjectSetter: 'setProduct')]
#[ORM\Entity]
#[ORM\EntityListeners([StoreListener::class])]
class Product implements HasStatesInterface
{
    #[ORM\Id, ORM\GeneratedValue, ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[AggregateSource(getter: 'getName', setter: 'setName')]
    #[ORM\Column(type: Types::STRING, length: 255)]
    private string $name;

    #[AggregateSource(getter: 'getPrice', setter: 'setPrice')]
    #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)]
    private string $price;

    #[ORM\OneToMany(targetEntity: ProductState::class, mappedBy: 'product', cascade: ['persist'])]
    private Collection $states;

    public function __construct(string $name, string $price) {
        $this->name = $name;
        $this->price = $price;
        $this->states = new ArrayCollection();
    }

    // --- Getters and Setters for tracked properties ---
    public function getName(): string { return $this->name; }
    public function setName(string $name): void { $this->name = $name; }
    public function getPrice(): string { return $this->price; }
    public function setPrice(string $price): void { $this->price = $price; }

    // --- Interface Implementation ---
    /** @return Collection<int, StateInterface> */
    public function getEventSourcingStates(): Collection { return $this->states; }

    public function getId(): ?int { return $this->id; }
}


// src/Entity/ProductState.php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Inwebo\DoctrineEventSourcing\Model\Interface\StateInterface;

#[ORM\Entity]
class ProductState implements StateInterface
{
    #[ORM\Id, ORM\GeneratedValue, ORM\Column(type: 'integer')]
    private ?int $id = null; // State entities usually need their own ID

    #[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'states')]
    #[ORM\JoinColumn(name: 'product_id', referencedColumnName: 'id', nullable: false)]
    private Product $product; // Relationship back to the Aggregate Root

    #[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
    private ?string $name = null;

    #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
    private ?string $price = null;

    // No constructor needed usually, states are created by the listener

    // --- Getters and Setters for tracked properties (matching AggregateSource) ---
    public function getName(): ?string { return $this->name; }
    public function setName(?string $name): void { $this->name = $name; }
    public function getPrice(): ?string { return $this->price; }
    public function setPrice(?string $price): void { $this->price = $price; }

    // --- Setter for the relationship (matches subjectSetter in AggregateRoot) ---
    public function setProduct(Product $product): void { $this->product = $product; }
    public function getProduct(): Product { return $this->product; }

    public function getId(): ?int { return $this->id; }
}