15/05/2015 - DOCTRINE, SYMFONY
Genel anlamda veritabanı ile çalışan uygulamaları test ederken, veritabanında bilgi olması gerekir. Symfony uygulamaları içinde biz bu işlemi, doctrine data fixtures özelliğini kullanarak otomatik hale getirebiliriz. Daha fazla bilgi için DoctrineFixturesBundle ve Doctrine2 ORM Data Fixtures Extensions sayfalarını okuyunuz.
"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(),
);
Örneğimizdeki "Country" ve "League" entitiler arasında 1 to N ilişki var.
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;
}
Entitiler arasında ilişki olduğunda, kod yazma tekrarını önlemek için fixtures içinde referanslandırma yapılır. Ayrıca Country fixtures ilk sırada çalıştırılmalıdır çünkü Country ana entitidir. Aşağıdaki addReference()
ve getReference()
satırlarına dikkat edin.
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
Careful, database will be purged. Do you want to continue Y/N ?y
> 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)