PHP code example of hostnet / entity-blamable-component
1. Go to this page and download the library: Download hostnet/entity-blamable-component 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/ */
hostnet / entity-blamable-component example snippets
use Acme\Bundle\AcmeBundle\Service\AcmeBlamableProvider;
use Hostnet\Component\EntityBlamable\Listener\BlamableListener;
use Hostnet\Component\EntityBlamable\Resolver\BlamableResolver;
use Hostnet\Component\EntityTracker\Listener\EntityChangedListener;
use Hostnet\Component\EntityTracker\Provider\EntityMetadataProvider;
use Hostnet\Component\EntityTracker\Provider\EntityMutationMetadataProvider;
/* @var $em \Doctrine\ORM\EntityManager */
$event_manager = $em->getEventManager();
// setup . This is an
// application specific implementation of the BlamableProviderInterface
$blamable_provider = new AcmeBlamableProvider();
// creating the blamable listener
$blamable_listener = new BlamableListener($blamable_resolver, $blamable_provider);
// register the events
$event_manager->addEventListener('preFlush', $entity_changed_listener);
$event_manager->addEventListener('entityChanged', $blamable_listener);
namespace Acme\Bundle\AcmeBundle\Service;
use Hostnet\Component\EntityBlamable\Provider\BlamableProviderInterface;
class AcmeBlamableProvider implements BlamableProviderInterface
{
public function __construct(private string $username)
{
}
public function getUpdatedBy(): string
{
return $this->username;
}
public function getChangedAt(): \DateTime
{
return new \DateTimeImmutable();
}
}
use Doctrine\ORM\Mapping as ORM;
use Hostnet\Component\EntityBlamable\Attributes\Blamable;
use Hostnet\Component\EntityBlamable\BlamableInterface;
#[ORM\Entity]
#[Blamable]
class MyEntity implements BlamableInterface
{
#[ORM\Column(nullable: true)]
private ?string $updated_by = null;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTime $updated_at = null;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTime $created_at = null;
public function setUpdatedBy(string $by): static
{
$this->updated_by = $by;
return $this;
}
public function setUpdatedAt(\DateTime $at): static
{
$this->updated_at = $at;
return $this;
}
public function setCreatedAt(\DateTime $at): static
{
$this->created_at = $at;
return $this;
}
}
$entity->setName('henk');
$em->flush();
// Voila, your updated_at and updated_by are filled in (and if it's new, created_at too).
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.