[TYPO3-mvc] Calling persistAll() inside a loop
Franz Koch
typo3.RemoveForMessage at elements-net.de
Wed Dec 29 19:05:35 CET 2010
On 29.12.2010 18:49, Kevin Ulrich Moschallski wrote:
> 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.
...
> 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?
how about simply implementing a category cache in your import method?
/**
* Your category cache
* @var array
*/
protected $categoryCache = array();
/**
* is resolving a category object by it's name.
* @var string $name The name of the category
* @return Tx_YourExt_Domain_Model_Category
*/
protected function getCategoryByName($name) {
if (!isset($this->categoryCache[$name])) {
$category = $this->categoryRepository->findOneByName($name);
if (!empty($category)) {
$this->categoryCache[$name] = $category;
} else {
$newCategory =
$this->createCategoryObject($referenceArray['categories']);
$this->categoryRepository->add($newCategory);
$this->categoryCache[$name] = $newCategory;
}
}
return $this->categoryCache[$name];
}
public function import() {
//your loop
if ($referenceArray['categories']) {
$userObject->setCategory(
$this->getCategoryByName($referenceArray['categories']));
}
}
The persistence in then only done once at the end of the import.
--
kind regards,
Franz Koch
More information about the TYPO3-project-typo3v4mvc
mailing list