Skip to content

Commit c695702

Browse files
committed
init migration to doctrine
1 parent 80fa850 commit c695702

File tree

13 files changed

+460
-31
lines changed

13 files changed

+460
-31
lines changed

.editorconfig

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
indent_size = 4
13+
trim_trailing_whitespace = false
14+
15+
[*.mk,Makefile]
16+
indent_style = tab
17+
18+
[*.{php,yml,yaml}]
19+
indent_size = 4

app/AppKernel.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function registerBundles()
2424
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
2525
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
2626
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
27+
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
2728
}
2829

2930
return $bundles;

app/config/parameters.yml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
parameters:
55
database_host: 127.0.0.1
66
database_port: ~
7-
database_name: symfony
7+
database_name: starwars
88
database_user: root
99
database_password: ~
1010
# You should uncomment this if you want use pdo_sqlite

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
},
3232
"require-dev": {
3333
"sensio/generator-bundle": "^3.0",
34-
"symfony/phpunit-bridge": "^3.0"
34+
"symfony/phpunit-bridge": "^3.0",
35+
"doctrine/doctrine-fixtures-bundle": "^2.3"
3536
},
3637
"scripts": {
3738
"post-install-cmd": [

composer.lock

+116-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace AppBundle\DataFixtures\ORM;
4+
5+
require_once __DIR__ . '/../../../../vendor/webonyx/graphql-php/tests/StarWarsData.php';
6+
7+
use Doctrine\Common\DataFixtures\FixtureInterface;
8+
use Doctrine\Common\Persistence\ObjectManager;
9+
use AppBundle\Entity\Character;
10+
use Doctrine\ORM\Mapping\ClassMetadata;
11+
use GraphQL\StarWarsData;
12+
13+
class LoadUserData implements FixtureInterface
14+
{
15+
/** @var Character[] */
16+
private $characters = [];
17+
18+
public function load(ObjectManager $manager)
19+
{
20+
$this->loadCharacters($manager, StarWarsData::humans(), Character::TYPE_HUMAN);
21+
$this->loadCharacters($manager, StarWarsData::droids(), Character::TYPE_DROID);
22+
$this->loadFriends($manager, array_merge(StarWarsData::humans(), StarWarsData::droids()));
23+
}
24+
25+
private function loadCharacters(ObjectManager $manager, array $characters, $type)
26+
{
27+
foreach($characters as $data) {
28+
unset($data['friends'], $data['appearsIn']);
29+
$data['type'] = $type;
30+
31+
$character = new Character();
32+
$character->fromArray($data);
33+
34+
$manager->persist($character);
35+
$metadata = $manager->getClassMetaData(get_class($character));
36+
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
37+
$manager->flush();
38+
39+
$this->characters[$character->getId()] = $character;
40+
}
41+
}
42+
43+
private function loadFriends(ObjectManager $manager, array $characters)
44+
{
45+
foreach($characters as $data) {
46+
$character = $this->characters[$data['id']];
47+
foreach ($data['friends'] as $friendId) {
48+
$friend = $this->characters[$friendId];
49+
50+
$character->addFriend($friend);
51+
}
52+
$manager->persist($character);
53+
$manager->flush();
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)