15/05/2015 - DOCTRINE, SYMFONY
Testing database driven applications requires existing data in database. In symfony applications, we can populate database with dummy data by using doctrine data fixtures feature. For more information read DoctrineFixturesBundle and Doctrine2 ORM Data Fixtures Extensions.
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "2.2.0",
"doctrine/data-fixtures": "1.1.1"
},
inanzzz-MBP:sport inanzzz$ composer update doctrine/doctrine-fixtures-bundle
inanzzz-MBP:sport inanzzz$ composer update doctrine/data-fixtures
# sport/app/AppKernel.php
$bundles = array(
...
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
);
There is a 1 to N relationship between "Country" and "League" entities.
namespace Football\FrontendBundle\Entity;
class Country
{
protected $id;
protected $code;
protected $name;
protected $createdAt;
/**
* @ORM\OneToMany(
* targetEntity="League",
* mappedBy="country",
* cascade={"persist", "remove"},
* orphanRemoval=true
* )
*/
protected $league;
}
namespace Football\FrontendBundle\Entity;
class League
{
protected $id;
protected $name;
protected $createdAt;
/**
* @ORM\ManyToOne(
* targetEntity="Country",
* inversedBy="league"
* )
* @ORM\JoinColumn(
* name="country_id",
* referencedColumnName="id",
* onDelete="CASCADE",
* nullable=false
* )
*/
protected $country;
}
When there are relationships between entities, relevant fixtures will have to have references between entities to avoid code repetitions. Also the Country fixtures must run first because the Country entity is the parent to League entity. Pay attention to addReference()
and getReference()
lines below.
namespace Football\FrontendBundle\DataFixtures\ORM;
use DateTime;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Football\FrontendBundle\Entity\Country;
class CountryFixtures extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
foreach ($this->getData() as $data) {
$country = $this->getCountry($data);
$this->addReference($data['reference'], $country);
$manager->persist($country);
}
$manager->flush();
}
private function getCountry(array $data)
{
return (new Country())
->setCode($data['code'])
->setName($data['name'])
->setCreatedAt(new DateTime($data['created_at']));
}
private function getData()
{
return
[
[
'reference' => 'germany',
'code' => 'DE',
'created_at' => '2015-05-16 10:19:09',
'name' => 'Germany',
],
[
'reference' => 'spain',
'code' => 'ES',
'created_at' => '2015-05-16 10:19:09',
'name' => 'Spain',
],
[
'reference' => 'turkey',
'code' => 'TR',
'created_at' => '2015-05-16 10:19:09',
'name' => 'Turkey',
],
];
}
public function getOrder()
{
return 1;
}
}
namespace Football\FrontendBundle\DataFixtures\ORM;
use DateTime;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Football\FrontendBundle\Entity\League;
class LeagueFixtures extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
foreach ($this->getData() as $data) {
$league = $this->getLeague($data);
$manager->persist($league);
}
$manager->flush();
}
private function getLeague(array $data)
{
return (new League())
->setName($data['name'])
->setCreatedAt(new DateTime($data['created_at']))
->setCountry($this->getReference($data['country']));
}
private function getData()
{
return
[
[
'reference' => 'germany-bundesliga',
'name' => 'Bundesliga',
'created_at' => '2015-05-16 10:19:09',
'country' => 'germany',
],
[
'reference' => 'spain-primera',
'name' => 'Primera División',
'created_at' => '2015-05-16 10:19:09',
'country' => 'spain',
],
[
'reference' => 'spain-segunda',
'name' => 'Segunda División',
'created_at' => '2015-05-16 10:19:09',
'country' => 'spain',
],
[
'reference' => 'turkey-super',
'name' => 'Süper Lig',
'created_at' => '2015-05-16 10:19:09',
'country' => 'turkey',
],
[
'reference' => 'turkey-tff',
'name' => 'TFF 1. Lig',
'created_at' => '2015-05-16 10:19:09',
'country' => 'turkey',
],
];
}
public function getOrder()
{
return 2;
}
}
inanzzz-MBP:sport inanzzz$ php app/console doctrine:fixtures:load --no-interaction --no-debug
> purging database
> loading Football\FrontendBundle\DataFixtures\ORM\CountryFixtures
> loading Football\FrontendBundle\DataFixtures\ORM\LeagueFixtures
mysql> SELECT * FROM country;
+----+---------+---------------------+------------+------+
| id | name | created_at | updated_at | code |
+----+---------+---------------------+------------+------+
| 16 | Germany | 2015-05-16 10:19:09 | NULL | DE |
| 17 | Spain | 2015-05-16 10:19:09 | NULL | ES |
| 18 | Turkey | 2015-05-16 10:19:09 | NULL | TR |
+----+---------+---------------------+------------+------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM league;
+----+------------+-------------------+---------------------+------------+
| id | country_id | name | created_at | updated_at |
+----+------------+-------------------+---------------------+------------+
| 1 | 16 | Bundesliga | 2015-05-16 10:19:09 | NULL |
| 2 | 17 | Primera División | 2015-05-16 10:19:09 | NULL |
| 3 | 17 | Segunda División | 2015-05-16 10:19:09 | NULL |
| 4 | 18 | Süper Lig | 2015-05-16 10:19:09 | NULL |
| 5 | 18 | TFF 1. Lig | 2015-05-16 10:19:09 | NULL |
+----+------------+-------------------+---------------------+------------+
5 rows in set (0.00 sec)