user3549136
user3549136

Reputation: 57

Doctrine Events, update persisted entity

i wrote an Importer script, which read entries from an csv file, and iterate the rows. To handle big files without performance loss, i insert new data within doctrine batch(bulks).

My problem at the moment is, i have an "Category" entity, which should be expanded only within new entries. So i have to check if entries are available given category names.

My first question is, i've read that doctrines "prePersist" event will be called on call "$entityManager->persist()" and inside the "flush" method (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#prepersist).

So how can i check if the event was inside an flush? The next thing, how can i update the actually entity within the identity datas?

I try to set the id, but without any effect.

<?php
/**
 * @return \Doctrine\Commong\Collections\ArrayCollection
 */
public function getCategories()
{
    if (null === $this->categories) {
        $this->categories = $this->getServiceCategory()->findAll();
    }
    return $this->categories;
}

public function prePersist(LifecycleEventArgs $event)
{
    $entity        = $event->getEntity();
    $objectManager = $event->getObjectManager();
    if ($entity instanceof \Application\Entity\Category) {
        $categories = $this->getCategories();

        $entityCriteria = // buildCriteria from Entity;
        $matched = $categories->matching($entityCriteria);

        if ($matched->count() > 0) {
            $entity->setId($matched->first()->getId();
        }
    }
}

So, here i dont know how to update the persisted categorie entity? Is this the right event, or should be an other event a better solution for my situation?

I developed the import within zf2 and doctrine2.

regards

Upvotes: 1

Views: 191

Answers (1)

Achim
Achim

Reputation: 322

First I would recommend to use DQL instead of ORM entities within you import script, because it makes you code much more simple.

Your import process increases the performance, if you first (1.) read all existing "Categories" from yor database, keep them within a member variable and second (2.) iterate each csv row and compare its category foreign key with the initially read set of categories.

If the category already exists in you database, create a new entity row with the existing corresponding foreign key, else create a new category and create a new entity row associated to the new category.

<?php 

// read categories from database
$categories = "..."; /* e.g. array('A' => 1,
                                   'B' => 2, ...); */

// ...

// Iterate each csv row 
foreach($csvRows as $csvRow) {

    // check category name
    if(!array_key_exists($csvRow['category']), $categories) {

        // Create new Category 

        // Remember id of the new created category
        $categoryId = "...";

    } else {

        // Use already existing category id
        $categoryId = $categories[$csvRow['category']];
    }

    // Create new csv row entity with category foreign key
    // ...
}

?>

Upvotes: 0

Related Questions