This course is archived!
02.
Processors: Do Custom Stuff While Loading
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This course is archived!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
4 Comments
in AppKernel.php I have the following declarations
I made CharacterProcessor filename in src\AppBundle\DatFixtures\ORM:
In services.yml I put
Every time I try to run 'app/console hautelook:fixtures:load' says:
I don't understand why is so. I forget to configure something or what?
We may clone the respective branch with 'git clone -b 2.Processors{Do_Custom_Stuff_While_Loading} git@github.com:petre-symfony/making-fixtures-awesome-with-Alice-knp-tutorial.git'
Hey Diaconescu ,
From the error I see that your preProcess() method must be compatible with "ProcessorInterface::preProcess(string $id, $object)" which means you have invalid method signature, i.e. you missed $id as the first argument. Check the preProcess() and postProcess() methods in "Fidry\AliceDataFixtures\ProcessorInterface" interface.
Cheers!
I saw that. Indeed in vendor/theofidry/alice-data-fixtures/src/ProcessorInterface file is this code:
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace Fidry\AliceDataFixtures;
/**
* Processor are meant to be used during the loading of files via a {@see LoaderInterface} in the scenario of having the
* loaded objects persisted in the database.
*
* @see \Fidry\AliceDataFixtures\Loader\PersisterLoader For an example of usage of processors
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
interface ProcessorInterface
{
/**
* Processes an object before it is persisted to DB.
*
* @param string $id Fixture ID
* @param object $object
*
* @return
*/
public function preProcess(string $id, $object);
/**
* Processes an object after it is persisted to DB.
*
* @param string $id Fixture ID
* @param object $object
*
* @return
*/
public function postProcess(string $id, $object);
}
Is true that they have an id parameter there.
But in vendor/theofidry/alice-data-fixtures/src/doc/processors.md they have an example how this processorInterface may be used:
# Processors
Processors allow you to process objects before and/or after they are persisted. Processors
must implement the [`Fidry\AliceDataFixtures\ProcessorInterface`](../src/ProcessorInterface.php).
Here is an example where we may use this feature to make sure passwords are properly
hashed on a `User`:
```php
namespace MyApp\DataFixtures\Processor;
use Fidry\AliceDataFixtures\ProcessorInterface;
use MyApp\Hasher\PasswordHashInterface;
use User;
final class UserProcessor implements ProcessorInterface
{
/**
* @var PasswordHashInterface
*/
private $passwordHasher;
/**
* @param PasswordHashInterface $passwordHasher
*/
public function __construct(PasswordHashInterface $passwordHasher)
{
$this->passwordHasher = $passwordHasher;
}
/**
* {@inheritdoc}
*/
public function preProcess($object)
{
if (false === $object instanceof User) {
return;
}
$object->password = $this->passwordHasher->hash($object->password);
}
/**
* {@inheritdoc}
*/
public function postProcess($object)
{
// do nothing
}
}
```
In Symfony, if you wish to register the processor above you need to tag it with the
`fidry_alice_data_fixtures.processor` tag:
```yaml
# app/config/services.yml
services:
alice.processor.user:
class: AppBundle\DataFixtures\Processor\UserProcessor
arguments:
- '@password_hasher'
tags: [ { name: fidry_alice_data_fixtures.processor } ]
```
Previous chapter: [Basic usage](../README.md#basic-usage)
Next chapter: [Purge data](purge_data.md)
As you may see in their example is only $object parameter. I must miss something. What? I created my code looking after their example. Teoretically should be worked. I don't understand how, because I saw this discrepancy myself.
Hey Diaconescu ,
That's mean their docs is outdated, so you have 2 ways:
1. Use the v1.0.0-beta.2 release which has the same signature of ProcessorInterface you see in examples,
2. Use the latest release version... and implement the new signature. Of course, feel free to open an issue about this discrepancy on their repo if it isn't yet, I bet guys who maintain the bundle will help you.
Cheers!
"Houston: no signs of life"
Start the conversation!