有什么方法可以知道 Doctrine 什么时候需要坚持|刷新?

Is there any way to know when Doctrine has something to persist|flush?

我正在尝试构建允许我插入|更新新实体的功能,但我在尝试应用软件开发的 DRY 原则时遇到问题,旨在减少各种信息的重复。这是我的:

foreach ($data as $row) {
        $entity = $this->_em->getRepository('PDOneBundle:Target')->findOneBy(
            array('veeva_account_id' => $row['Id'])
        );

        $lastModifiedAt = new \DateTime($row['LastModifiedDate']);

        // Constraints
        if ($row['Id'] != null && $row['FirstName'] != null && $row['LastName']) {
            // CREATE
            if (!is_object($entity)) {
                $entity = new Entity\Target();
                $entity->setVeevaAccountId($row['Id']);

                $entity->setVeevaAccountId($row['Id']);
                $entity->setNpi($row['NPI_vod__c']);
                $entity->setFirst(ucfirst(strtolower($row['FirstName'])));
                $entity->setLast(ucfirst(strtolower($row['LastName'])));
                $entity->setTitle($row['Title__c']);
                $entity->setDisplayName(
                    ucfirst(strtolower($row['FirstName'])).' '.ucfirst(
                        strtolower($row['LastName'])
                    )
                );
                $entity->setLastSyncAt(new \DateTime());

                // Persisting the current user
                $this->_em->persist($entity);

                // Each 1000 items persisted we flush everything
                if (($i % $this->_batchSize) === 0) {
                    echo 'Flushing batch...'."\n";
                    echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";

                    $this->_em->flush();
                    $this->_em->clear();

                    echo 'After batch...'."\n";
                    echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
                }
                ++$i;
            }

            $lastSyncAt = $entity->getLastSyncAt();

            // UPDATE
            if ($lastModifiedAt > $lastSyncAt) {
                // Updating info
                $entity->setVeevaAccountId($row['Id']);
                $entity->setNpi($row['NPI_vod__c']);
                $entity->setFirst(ucfirst(strtolower($row['FirstName'])));
                $entity->setLast(ucfirst(strtolower($row['LastName'])));
                $entity->setTitle($row['Title__c']);
                $entity->setDisplayName(
                    ucfirst(strtolower($row['FirstName'])).' '.ucfirst(
                        strtolower($row['LastName'])
                    )
                );
                $entity->setLastSyncAt(new \DateTime());

                // Persisting the current user
                $this->_em->persist($entity);

                // Each 1000 items persisted we flush everything
                if (($i % $this->_batchSize) === 0) {
                    echo 'Flushing batch...'."\n";
                    echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";

                    $this->_em->flush();
                    $this->_em->clear();

                    echo 'After batch...'."\n";
                    echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
                }
                ++$i;
            }
        }
    }

您可能会注意到这段代码:

$this->_em->persist($entity);

// Each 1000 items persisted we flush everything
if (($i % $this->_batchSize) === 0) {
    echo 'Flushing batch...'."\n";
    echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";

    $this->_em->flush();
    $this->_em->clear();

    echo 'After batch...'."\n";
    echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
}
++$i;

出现了两次,然后我想到的是检查 Doctrine 是否有东西要坚持|刷新然后拿出代码并放在条件下面但我不知道这是否可能或者如何,所以有什么建议吗?

PS:欢迎更改代码逻辑,随时给我提出建议,以实现同样的目标并改进我的代码

如评论中所述,您可以使用学说 prePersist/preUpdate 事件(文档:http://doctrine-orm.readthedocs.org/en/latest/reference/events.html)。

如果你只是想保持干爽,我也可以推荐你这样的功能:

protected function flushBatch($em, $counter, $batchSize) {
    if (($counter % $batchSize) === 0) {
        echo 'Flushing batch...'."\n";
        echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";

        $em->flush();
        $em->clear();

        echo 'After batch...'."\n";
        echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
    }
}

显然,如果您在项目的其他任何地方都不需要逻辑,那么这只是一个建议。