Php 实体经理在Symfony上的表现

Php 实体经理在Symfony上的表现,php,symfony,doctrine,Php,Symfony,Doctrine,我必须为数据迁移制作一个命令,但是执行时间太长,并且在长时间后崩溃,我需要使它更快,你能帮助我吗?这里有一个例子: 处境 我有一个问题实体 我有一个客户实体 我有一个MatterCastomer实体 物质有一个或多个MatterCustomer 客户可以是无业务或多业务客户 请注意,实体之间的链接设置正确 命令 function execute(InputInterface $input, OutputInterface $output) { // Here connection t

我必须为数据迁移制作一个命令,但是执行时间太长,并且在长时间后崩溃,我需要使它更快,你能帮助我吗?这里有一个例子:

  • 处境

    • 我有一个问题实体
    • 我有一个客户实体
    • 我有一个MatterCastomer实体
    • 物质有一个或多个MatterCustomer
    • 客户可以是无业务或多业务客户
      请注意,实体之间的链接设置正确
  • 命令

    function execute(InputInterface $input, OutputInterface $output)
    {
        // Here connection to old database
        $this->pdo = $this->container()->get('app.command_oldbase.odbase_helper')->connectToDatabase();
        $em = $this->getContainer()->get('doctrine')->getManager();
        $em->getConnection()->beginTransaction()
        $em->getConnection()->setAutoCommit(false);
        try{
            $this->migrateMatters($output);
        } catch (Exception $e) {
            $em->getConnection()->rollback();
            throw $e;
        }
        $em->getConnection()->commit();
    }
    
    function migrateMatters(OutputInterface $output)
    {
        $statement = $this->pdo->prepare('Select id,name From oldmatter');
        $statement->execute();
        $oldmatters = $statement->fetchAll();
    
        foreach ($oldmatters As $oldmatter) {
           $this->em = $this->getContainer()->get('doctrine')->getManager();
           $matter = (new Matter())
               ->setName($oldmatter['name'])
           ;
           $this->em->persist($matter);
           $this->em->flush();
    
           $this->setMatterCustomer($matter, $oldmatter['id']);
    
           $this->em->clear();
        }
    }
    
    function setMatterCustomer(Matter $matter, int $oldmatter_id)
    {
        $statement = $this->pdo->prepare('Select customername From oldmattercustomers Where oldmatter_id = :oldmatter_id');
        $statement->bindParam(':oldmatter_id', $oldmatter_id);
        $statement->execute();
        $oldcustomers = $statement->fetchAll();
    
        foreach ($oldcustomers As $oldcustomer) {
            //search customer in Customer entity 
            $customer = $this->em->getRepository(Customer::class)->findOneBy(['name' => $oldcustomer['customername']]);
    
            //search if MatterCustomer exist
            $matterCustomer = $this->em->getRepository(MatterCustomer::class)->findOneBy(['matter' => $matter, 'customer' => $customer]);
    
            if ($matterCustomer == null) {
                $matterCustomer = (new MatterCustomer())
                   ->setContact($contact)
                   ->setMatter($matter)
                ;
                //add MatterCustomer to arrayCollection in Matter
                $matter->addCustomer($matterCustomer);
                $this->em->persist($matter);
                $this->em->flush();
            }
        }
     }
    
  • 使用

  • 用于客户事务。所以,首先您需要创建Matter实例,然后添加到MatterCastomer实例,以此类推。然后执行持久化,当然只执行一个实例

  • 如果存在多个实体,那么禁用日志记录可能会减少内存消耗

  • $this->em->getConnection()->getConfiguration()->setSQLLogger(null)

  • 使用
    --no debug
    选项(或在
    prod
    模式下)运行命令,将减少内存消耗