PHP code example of adt / doctrine-loggable

1. Go to this page and download the library: Download adt/doctrine-loggable 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/ */

    

adt / doctrine-loggable example snippets




use Doctrine\ORM\Mapping as ORM;
use ADT\DoctrineLoggable\Attributes as ADA;

#[ORM\Entity]
#[ADA\LoggableEntity]
class User
{
	#[ORM\Column(nullable: true)]
	#[ADA\LoggableProperty]
	protected ?string $firstname = null;

	#[ORM\ManyToMany(targetEntity: Role::class, inversedBy: 'users')]
	#[ADA\LoggableProperty]
	protected Collection $roles;
}

#[ORM\Entity]
#[ADA\LoggableIdentification(fields: ['name'])]
class Role
{
	#[ORM\Column]
	protected string $name;

	#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'roles')]
	protected Collection $users;
}

#[ORM\Column(nullable: true)]
#[ADA\LoggableProperty(withValue: false)]
protected ?string $password = null;

class ChangeAuditSubscriber
{
	public function logEntry(ChangeLog $logEntry, object $entity, bool $announced): void
	{
		if (!$entity instanceof User) {
			return;
		}

		$this->audit->record(
			$logEntry->getObjectClass(),
			$logEntry->getObjectId(),
			$logEntry->getChangeSet(),
		);
	}
}

use ADT\DoctrineLoggable\Serializer\ValueHandler;
use ADT\DoctrineLoggable\Serializer\ValueSerializer;

class MoneyHandler implements ValueHandler
{
	public function getType(): string
	{
		return 'money';
	}

	public function supports(mixed $value): bool
	{
		return $value instanceof Money;
	}

	public function encode(mixed $value, ValueSerializer $serializer): array
	{
		return ['amount' => $value->getAmount(), 'currency' => $value->getCurrency()];
	}

	public function decode(array $data, ValueSerializer $serializer): Money
	{
		return new Money($data['amount'], $data['currency']);
	}
}