KnpLabs/geekweek12 — pull request #18 by gquemener (2012-11-20 11:48:44+00:00)
Title: [WIP] Added a rad base doctrine data fixtures class

# A RAD AbstractFixture for Doctrine Data Fixtures
## Really? Why?

Because, I think it's sad to always create entity class fixtures loader with the same code inside:
``` php
class MyEntity extends AbstractFixture
{
private function createMyEntity(ObjectManager $manager, $name, $type, $otherStuff)
{
$myEntity = new MyEntity();
$myEntity->setName($name);
$myEntity->setType($type);
$myEntity->setOtherStuff($otherStuff);

$manager->persist($myEntity);
$this->addReference(sprintf('MyEntity:%s', $name);
}

public function load(ObjectManager $manager)
{
$this->createMyEntity($manager, 'foo', 'bar', 'baz');
$this->createMyEntity($manager, 'goo', 'car', 'caz');
$this->createMyEntity($manager, 'hoo', 'dar', 'daz');
}
}
```

## Oh... How then?
By providing a `Knp\RadBundle\DataFixtures\AbstractFixture` class.

It will provide a `createObject` method:
```php
class MyEntity extends AbstractFixture
{
public function load(ObjectManager $manager)
{
$this->createObject($manager, 'App\Entity\MyEntity', ['name' => 'foo', 'type' => 'bar', 'otherStuff' => 'baz']);
$this->createObject($manager, 'App\Entity\MyEntity', ['name' => 'goo', 'type' => 'car', 'otherStuff' => 'caz']);
$this->createObject($manager, 'App\Entity\MyEntity', ['name' => 'goo', 'type' => 'dar', 'otherStuff' => 'daz']);
}
}
```

And create reference using a simple convention.

## Reference naming convention
```php
$this->createObject($manager, 'App\Entity\MyEntity', ['name' => 'foo', 'type' => 'bar', 'otherStuff' => 'baz']);
```
will generate reference `MyEntity:foo` (because `foo` is the value of the first attribute).

if `MyEntity:foo` is already registered, then it will try to register `MyEntity:foo-1` etc...

In case which there is no attributes, then it will try to create reference `MyEntity`, `MyEntity:1`, `MyEntity:2`, etc...

## TODO
The Reference Manipulator is working. I still have to figure out how to inject it into the rad `AbstractFixture` class.

Do you think it's a usefull feature?