TYPO3 set uid to new model

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



TYPO3 set uid to new model



Simple question: how to set uid to new model? TYPO3 8.7.17.
I tried adding method


public function setUid($uid)

$this->uid = $uid;



I assume I am creating a custom extension and I want to save the uid in a new model
But model not saving. I know it is not recommend. But I have causes why I need it.



I need id because user make import of data from csv every day. And I have relative data. When user make import uids are changing. I can't assign the date as well.





I assume you are creating a custom extension and you want to save the uid in a model, aren't you?
– HerrSerker
Aug 7 at 14:31






exactly what I need
– Mikael
Aug 7 at 14:32





You should elaborate on the causes why [you] need it. Otherwise we cannot be sure that there is or isn't a better solution.
– Mathias Brodala
Aug 7 at 14:43




2 Answers
2



Setting the uid is not possible that easily, but you can use the TYPO3CMSExtbasePersistenceGenericMapperDataMapper to create the entity instance from a given data array.


uid


TYPO3CMSExtbasePersistenceGenericMapperDataMapper


$data = [
'uid' => 10101,
'title' => 'Foo Bar Baz',

];

$dataMapper = GeneralUtility::makeInstance(ObjectManager::class)
->get(DataMapper::class);

$obj = reset($dataMapper->map(YourModel::class, [data]));



Or you just create a new Instance of the Model and use Reflection to change the uid property:


$obj = new YourEntity();
$refl = new ReflectionClass(YourEntity::class);

$refl->getProperty('uid')->setValue($obj, '10101');



In case of an Import I would prefer the former way, since you somehow need to map the data to the instance and that is what the DataMapper is for.



For the other way around you might have a look here



The function setUid() exists in the model already if I don't mistake, usually it's just in the class that you extend by your own model-class.

So you can use probably already setUid() without writing it in your own model-class.


setUid()


setUid()



The main problem is that uid is an auto_increment, index and primary key, so you've to assure that new values for uid are always unique.

Also the individual usage of setUid() might be still blocked somehow in TYPO3.


uid


auto_increment


setUid()



For import-related challenges there exists the extension external_import, that might be what you need.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard