In DoctrineFixturesBundle if I want execute fixtures in specified order i have to create in any fixture class method like this:
public function getOrder()
{
return 2;
}
It isn't comfortable way to specify order.
In Laravel Framework I can Do this like this:
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call('PositionsSeeder');
$this->call('UsersSeeder');
$this->call('PostsSeeder');
}
}
My question is: can I do it in this way in symfony?
I see one solution. I can make BaseFixture with this code:
class LoadBrandData extends AbstractFixture
{
public function load(ObjectManager $manager)
{
(new LoadPositionsData())->load(manager);
(new LoadUsersData())->load(manager);
(new LoadPostsData())->load(manager);
}
}
But maybe i can do this better. Can I?
You can use Doctrine DependentFixtureInterface; please check below example:
namespace MyDataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
class MyFixture extends AbstractFixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager)
{}
public function getDependencies()
{
return array('MyDataFixtures\MyOtherFixture'); // fixture classes fixture is dependent on
}
}
class MyOtherFixture extends AbstractFixture
{
public function load(ObjectManager $manager)
{}
}
Source: https://github.com/doctrine/data-fixtures
Even if you don't like it, the best way to achieve this is through the getOrder() method.
Having said that, there are at least two tricks you can use to set the order in other way:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With