Skip to content

Latest commit

 

History

History
68 lines (54 loc) · 1.63 KB

processors.md

File metadata and controls

68 lines (54 loc) · 1.63 KB

Processors

Processors allow you to process objects before and/or after they are persisted. Processors must implement the ProcessorInterface.

Here is an example where we may use this feature to make sure passwords are properly hashed on a User:

namespace Acme\DemoBundle\DataFixtures\ORM;

use Nelmio\Alice\ProcessorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use User;

class UserProcessor implements ProcessorInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * {@inheritdoc}
     */
    public function preProcess($object)
    {

    }

    /**
     * {@inheritdoc}
     */
    public function postProcess($object)
    {
        if (!($object instanceof User)) {
            return;
        }

        $hasher = $this->container->get('example_password_hasher');
        $object->password = $hasher->hash($object->password);
    }
}

You can add a list of processors in the load method, e.g.

$objects = \Nelmio\Alice\Fixtures::load(__DIR__.'/fixtures.yml', $objectManager, $options, $processors);

Or, you can add them to your loader using the addProcessor() method, e.g.

$loader = new \Nelmio\Alice\Fixtures($objectManager, $options);
$loader->addProcessor($processor);
$objects = $loader->loadFiles(__DIR__.'/fixtures.yml');

Previous chapter: Customize Data Generation
Got back to Table of Contents