1. Go to this page and download the library: Download orbitale/array-fixture 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/ */
orbitale / array-fixture example snippets
composer
namespace AppBundle\DataFixtures\ORM;
use App\Entity\Post;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class PostFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
$post = new Post();
$post->setTitle('First post');
$post->setDescription('Lorem ipsum');
$manager->persist($post);
$manager->flush();
}
}
namespace AppBundle\DataFixtures\ORM;
use App\Entity\Post;
use Orbitale\Component\ArrayFixture\ArrayFixture;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
class PostFixtures extends ArrayFixture implements ORMFixtureInterface
{
public function getEntityClass(): string
{
return Post::class;
}
public function getObjects(): iterable
{
yield ['title' => 'First post', 'description' => 'Lorem ipsum'];
yield ['title' => 'Second post', 'description' => 'muspi meroL'];
}
}
namespace App\DataFixtures\ORM;
use App\Entity\Tag;
use Orbitale\Component\ArrayFixture\ArrayFixture;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
class TagFixtures extends ArrayFixture implements ORMFixtureInterface
{
public function getEntityClass(): string
{
return Tag::class;
}
public function getReferencePrefix(): ?string
{
return 'tags-';
}
public function getMethodNameForReference(): string
{
return 'getName';
}
public function getObjects(): array
{
return [
['name' => 'Some tag'],
];
}
}
namespace App\DataFixtures\ORM;
use App\Entity\Post;
use Orbitale\Component\ArrayFixture\ArrayFixture;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
class PostFixtures extends ArrayFixture implements ORMFixtureInterface
{
public function getEntityClass(): string
{
return Post::class;
}
public function getObjects(): array
{
return [
[
'title' => 'First post',
'tags' => [
$this->getReference('tags-Some tag'),
],
],
];
}
}
namespace App\DataFixtures\ORM;
use App\Entity\Post;
use Orbitale\Component\ArrayFixture\ArrayFixture;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;
class PostFixtures extends ArrayFixture implements ORMFixtureInterface
{
public function getEntityClass(): string
{
return Post::class;
}
/**
* With this, we can retrieve a Post reference with this method:
* $this->getReference('posts-1');
* where '1' is the post id.
*/
public function getReferencePrefix(): ?string
{
return 'posts-';
}
protected function flushEveryXIterations(): int
{
return 1;
}
public function getObjects(): array
{
return [
['id' => 1, 'title' => 'First post', 'parent' => null],
[
'title' => 'Second post',
'parent' => function(Post $object, ArrayFixture $fixture, EntityManagerInterface $manager) {
return $fixture->getReference('posts-1');
},
],
];
}
}