[TYPO3-mvc] Get last id

Franz Koch typo3.RemoveForMessage at elements-net.de
Mon Jan 17 16:28:30 CET 2011


Hey,

>> Also you don't have to persist manually if you
>> don't need to work with the UID of the object directly - you can also
>> assign new objects directly to other objects and those get updated
>> automatically when persisted (you still have to add the new object to
>> the repository though).
>
> Can you say something more about that ? I thought that I need to persist
> everything to "save" changes. I don`t get the idea of assigning new
> object to existing one if - for example - I want to import 4-5 objects
> of type Record.

extbase is persisting automatically at the "shutdown" of your extension 
- so you don't have to take care of it in general. Only if you instantly 
need access to the uid of a newly created object (because you need to 
create a link to it or similar), then you have to trigger persistence 
manually. You also can directly use and assign new objects to other 
objects without knowing their UID. So let's say if you import portals 
and records in the same import process, neither of them is persisted and 
has a uid yet, but you can still work with them using a internal cache 
to always get the correct portal back, even if it hasn't been persisted 
yet. Here's a example.


foreach ($recordsToImport as $record) {
	$newRecord = t3lib_div::makeInstance('Tx_..._Record');
	$newRecord->setName($record['name']);
	$newRecord->setDescription($record['description']);

	foreach ($record['portals'] as $rawPortalData) {
		$portal = $this->resolvePortal($rawPortalData);
		if (is_object($portal)) {
			$newRecord->add($portal);
		}
	}
	$this->recordRepository->add($newRecord);
}

....
private function resolvePortal( $portalData ) {
	$name = $portalData['name'];
	// in order to be able to assign the very same portal to
	// imported records, even if it's newly created, use a
	// internal cache for resolving the portal objects
	// indpendent of persistence
	if (!isset($this->cache['portals'][$name])) {
		$portal = $this->portalRepository->findOneByName($name);
		if (!is_object($portal)) {
			$portal = $this->createNewPortal($portalData);
		}
		$this->cache['portals'][$name] = $portal;
	}
	return $this->cache['portals'][$name];
}
....
private function createNewPortal( $portalData ) {
	$portal = t3lib_div::makeInstance('Tx_..._Portal');
	$portal->setName($portalData['name']);
	...
	$this->portalRepository->add($portal);
}


Hope you got the point.
-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list