How to save entity and get last insert id

Hello
I’m trying to save an entity and then get new entity id but the entity will not update id after save from database.
In Yii2 active record models will update after save new record .
So how can I do this with Yii3 cycle orm entities?

I’m using this code in my repository file
$this->entityWriter->write([$entity]);

Is there a way to update entity after save automatically?

thanks

@roxblnfk ?

The entity id should be updated.
If it is not updated then you can create an issue in the cycle/orm repository with an example code.
Or you can write an example here and i will try help you. I need see code of the $entity class.

2 Likes

Hello
thanks for your kind response.
I was working on my application .
I think cycle have some issues.
I have a db in postgres with 14 tables and each table has maximum 300 records now.
I’m using custom primary keys (random strings with crypto extension). So based on this page cycle can’t support our primary keys to load automatically after save


Compound Primary Keys

ORM relies on a unique primary key to create a proper entity map, using complex primary keys is currently not implemented.

See https://github.com/cycle/orm/issues/22


Also I have another problem in my application, cycle orm take more than 1.5s to load cycle libraries because of my entities and relations
I will exit my application right after this step in Application runner and it takes 1.5s

var_dump(microtime(true));
$container = new Container(
     $config->get('web'),
     $config->get('providers-web')
);
var_dump(microtime(true));die;

look at this link please, the issue is still open

can you send me a link for cycle performance things that i can add to have better speed please?
thank you

can you send me a link for cycle performance things that i can add to have better speed please?

First, read about Schema caching. I think now you are does not use it.

I’m using custom primary keys (random strings with crypto extension). So based on this page cycle can’t support our primary keys to load automatically after save

Random string keys (UUID) is not same as Compound PK. Compound Primary Keys (or Composite PK) are multi-column keys.
Therefore, most likely a bug is either in the Cycle code or in your application. Let’s find out.
The best way is to write a test for cycle/orm that reproduces the problem.

1 Like