[TYPO3-mvc] Get last id

Dawid Pacholczyk dpacholczyk at gmail.com
Mon Jan 17 15:06:01 CET 2011


W dniu 2011-01-17 14:31, Franz Koch pisze:
> Hey,
>
>> How to get last inserted id.
>>
>> Is there a method in extbase simillar to mysql_insert_id ??
>>
>> I`ve used it but it gives me a strange result.
>> Id of last inserted id is 14 and i get 881.
>>
>> Any ideas ?
>
> you can't. Objects are persisted at the end of the extensions lifecycle
> and in a random order. If you need the uid of a new object for further
> processing you first have to trigger persistence manually, then your
> objects themselves will contain the correct UID already and you don't
> have to grab it from MySql.
>
> // add the object to the repository
> $yourRepository->add($someNewObject);
>
> // then persist everything
> $persistenceManager =
> $this->objectManager->get('Tx_Extbase_Persistence_Manager');
> $persistenceManager->persistAll();
>
> // now your object has a valid identity/uid
> var_dump ($someNewObject->getUid());
>

Ok I get it. But when I add in this way a new record to database it is 
inserted with out pid :/

I do something like that

$tempRecord = t3lib_div::makeInstance('Tx_MyExt_Domain_Model_Record');
			$tempRecord->setRUid($val['uid']);
			$tempRecord->addPortal($portalRepository->findByUid(13));
			$tempRecord->setTableName($table);
			$this->add($tempRecord);
			$persistenceManager->persistAll();

It works great only the pid isn`t set

Best regards,
Dawid Pacholczyk


More information about the TYPO3-project-typo3v4mvc mailing list