PHP code example of tourze / doctrine-track-bundle

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

    

tourze / doctrine-track-bundle example snippets


return [
    // ...
    Tourze\DoctrineTrackBundle\DoctrineTrackBundle::class => ['all' => true],
];



use Doctrine\ORM\Mapping as ORM;
use Tourze\DoctrineTrackBundle\Attribute\TrackColumn;

#[ORM\Entity]
class User
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[TrackColumn]  // This field will be tracked
    #[ORM\Column]
    private string $email;

    #[TrackColumn]  // This field will be tracked
    #[ORM\Column]
    private string $username;

    #[ORM\Column]   // This field will NOT be tracked
    private \DateTime $lastLogin;
    
    // ... getters and setters
}

// Create
$user = new User();
$user->setEmail('[email protected]');
$user->setUsername('john_doe');
$entityManager->persist($user);
$entityManager->flush(); // Logs: action='create', data=['email' => '[email protected]', 'username' => 'john_doe']

// Update
$user->setEmail('[email protected]');
$entityManager->flush(); // Logs: action='update', data=['email' => ['old' => '[email protected]', 'new' => '[email protected]']]

// Delete
$entityManager->remove($user);
$entityManager->flush(); // Logs: action='remove', data=['email' => '[email protected]', 'username' => 'john_doe']

use Tourze\DoctrineTrackBundle\Entity\EntityTrackLog;

// Find all changes to a specific entity
$logs = $entityManager->getRepository(EntityTrackLog::class)->findBy([
    'objectClass' => User::class,
    'objectId' => '123'
]);

// Find all changes by a specific action
$createLogs = $entityManager->getRepository(EntityTrackLog::class)->findBy([
    'action' => 'create'
]);

#[ORM\Entity]
class MyEntity
{
    #[ORM\Id]  // Required for tracking
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;
    
    // ... other properties
}
bash
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate