有什么方法可以知道Doctrine什么时候有什么东西要坚持下去?

时间:2015-06-24 13:21:36

标签: php symfony doctrine-orm symfony-2.6

我正在尝试构建一个允许我插入|更新新实体的函数,但是我在尝试应用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:欢迎代码逻辑更改,随时向我提出建议以实现相同并改进我的代码

1 个答案:

答案 0 :(得分:2)

如评论中所述,您可以使用doctrines 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";
    }
}

显然,如果你不需要项目中任何其他地方的逻辑,这只是一个建议。