I want to create fixture, that generate 10 records in my table (test0-test9), then create migration, wherein I need to rename records, that was created by fixture to (category0-category9). I have created this fixture:
class AppFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
for ($i = 0; $i < 10; $i++)
{
$product = new Category();
$product->setName('test '.$i);
$id = mt_rand(89,140);
$parent = $manager->getRepository(Category::class)->find($id);
$product->setParent($parent);
$manager->persist($product);
}
$manager->flush();
}
}
But how I can rename this records in doctrine using migration? Any idea? * I think, I need to create sql queries directly in my migration class...or not
UPDATE
I try to do this, but I think it is bad solution...
public function postUp(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
for ($i = 0; $i < 10; $i++)
{
$category = 'category '.$i;
$test = 'test'.$i;
$this->addSql('UPDATE category SET NAME = '.$category.' WHERE NAME = '.$test );
}
}
You can inject the entity manager inside your migration. Then you can find the object you want to change and change it. I hope this answers your question
Example:
public function postUp(Schema $schema)
{
$em = $this->container->get('doctrine.orm.entity_manager');
$products = $em->getRepository(Product::class)
->getProductsCustomQuery();
foreach($product as $products){
$product->setName(Whatever);
}
$em->flush();
}
Check this link
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.