PHP code example of hostnet / entity-mutation-component

1. Go to this page and download the library: Download hostnet/entity-mutation-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-mutation-component example snippets



use Hostnet\Component\EntityMutation\Listener\MutationListener;
use Hostnet\Component\EntityMutation\Resolver\MutationResolver;
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 on
// and which entity represents your Mutation entity.
$mutation_resolver = new MutationResolver($entity_metadata_provider);

// creating the mutation listener
$mutation_listener = new MutationListener($mutation_resolver);

// register the events
$event_manager->addEventListener('preFlush', $entity_changed_listener);
$event_manager->addEventListener('entityChanged', $mutation_listener);



use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Hostnet\Component\EntityMutation\Attributes\Mutation;
use Hostnet\Component\EntityMutation\MutationAwareInterface;

// The strategy below is the default, only shown for illustration.
#[ORM\Entity]
#[ORM\Table(name: 'users')]
#[Mutation(strategy: Mutation::STRATEGY_COPY_PREVIOUS)]
class MyUserEntity implements MutationAwareInterface
{
    ...
    private $id;

    #[ORM\Column(...)]
    private string $city;

    #[ORM\Column(...)]
    private string $name;

    #[ORM\OneToMany(...)]
    private Collection $mutations;

    public function setName(string $name): void { ... }
    public function getName(): string { ... }

    #[\Override]
    public function addMutation(object $mutation): void
    {
        $this->mutations->add($mutation);
    }

    #[\Override]
    public function getMutations(): iterable
    {
        return $this->mutations;
    }

    /**
     * Used to get the last mutation stored, you might want to change
     * it to return the one before that if your strategy is current.
     */
    #[\Override]
    public function getPreviousMutation(): ?object
    {
        $criteria = (new Criteria())
            ->orderBy(['id' => Criteria::DESC])
            ->setMaxResults(1);

        return $this->mutations->matching($criteria)->current() ?: null;
    }
}



use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class MyUserEntityMutation
{
    ...

    #[ORM\ManyToOne(targetEntity: MyUserEntity::class, inversedBy: 'mutations')]
    private MyUserEntity $user;

    #[ORM\Column(...)]
    private string $name;

    public function setName(string $name): void { ... }
    public function getName(): string { ... }

    public function __construct(MyUserEntity $user, MyUserEntity $original_data)
    {
        // link our user to the mutation
        $this->user = $user;

        // populate the mutation with data from the previous state
        $this->name = $original_data->getName();
    }
}




$my_user_entity->setName('Henk'); // was Hans before
$em->flush();
var_dump($my_user_entity->getPreviousMutation()); // shows the state it had with Hans