[TYPO3-mvc] Calling persistAll() inside a loop

Kevin Ulrich Moschallski km at 3digit.de
Wed Dec 29 18:49:23 CET 2010


Hi list,

i'm currently trying to import a list of references via soap.

I loop through all results from soap an add the references. Each 
reference object contains a fe_user object, an tt_address object and a 
reference category object.

The categories are created inside the same loop and here comes my 
problem. I check via the reference category respository if a category 
already exist, if not a create a new object and do a repository->add().

I noticed, that i get duplicated categories because when the next run 
of the loop comes to the point where it checks for the category the 
category isn't persisted yet. Sure this is because the controller calls 
persistAll() only at the end of each call.

So i tried to call persistAll() after i added the category to the 
repository which leads me to two problems:

- First, the performance isn't very nice.
- Second, it seems that my fe_users are persisted more than one time

Here is my code:

// check if a category with the Name of the limbas category(19) exist
                    if ($referenceArray['categories']) {
                      if ($referenceArray['categories'] != '1') {
                      $categories = 
$this->categoryRepository->findOneByName($referenceArray['categories']);
                      }
                    }

					if(!empty($categories)) {
                      $referenceArray['categories'] = $categories;
                    } else {
                      if ($referenceArray['categories']) {
                        if ($referenceArray['categories'] != '1') {
                        $newCategory = 
$this->createCategoryObject($referenceArray['categories']);
                        $this->categoryRepository->add($newCategory);
                        $persistenceManager = 
$this->objectManager->get('Tx_Extbase_Persistence_Manager');
                        $persistenceManager->persistAll();
                        $referenceArray['categories'] = $newCategory;
                        }
                      }
                    }

My question now is, is there a other approach to persist inside a loop? 
Could i add a new category controller and call this to add a new 
category? Maybe with DI?

Thanks for help in advance.

Regards,

Kevin



More information about the TYPO3-project-typo3v4mvc mailing list