我想要创建一个夹具,它在我的表中生成10个记录(test0-test9),然后创建迁移,其中我需要重命名记录,这是由夹具创建的(类别0-类别9)。我已经创建了这个装置:
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();
}
}但是,我怎样才能用迁移来重命名这些记录呢?我想,我需要在迁移class...or中直接创建sql查询,而不是
更新
我试着这么做,但我觉得这不是个好办法.
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 );
}
}发布于 2018-03-27 18:31:01
您可以在迁移中插入实体管理器。然后,您可以找到要更改的对象并对其进行更改。我希望这能回答你的问题
示例:
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();
}检查这个链接
https://stackoverflow.com/questions/49519036
复制相似问题