Yii2-pluto : failed to save user profile

I have a function that in the case of afterSave creates an object assigns a value to the primary key and tries to save it, but returns me

Integrity constraint violation: 1062 Duplicate entry '17' for key 'PRIMARY'
    The SQL being executed was: INSERT INTO `user_profile` (`id`) VALUES (17)

Hi,

Unfortunately this is too less information.
Do I understand your description correct?

  1. You save a new record.
  2. Inside $myRecord->afterSave() you try to overwrite / change the primary key (of the very same record you just created in Step 1)?

Could you show some example code?
It would help to determine the actual problem.

Regards

In register action I save User model, in same model I’ve an afterSave( ) that create a UserProfile class, copy user->id to userProfile->id and then return userProfile->save(false)

Where is that wrong? Thanks

p.s
here the model : https://github.com/sjaakp/yii2-pluto/blob/master/models/User.php

Can be a solution, first save the model (profile) and then update the primary key with $model->updateAttributes([…]); ?

Hi,

Sorry, currently no time to investigate your code.

Just to ensure there is no missunderstanding:

  1. You want to create a user profile, together with the user, right?
  2. The primary key of the profile should be the id of the user, right? (user_profile.id = user.id)

“Integrity constraint violation: 1062 Duplicate entry ‘17’”

Means:The code tried to create a new profile with id=17,
but inside the user_profile table was already a record with id = 17.

This leads to following conclusions / questions:

  1. When there is already a user_profile record, where was it created initially?
  2. Is is possible that you forgot to truncate the user_profile table before testing your code?
  3. In your user_profile table, how is the primary key managed? Just “manually” by your code?

Best Regards

in the afterSave method of the User model with the statement $profile = Yii::createObject($cls, $prClass);

I tried to empty the table but I have the same result :frowning:

Hi @paskuale,

So you are using sjaakp/ yii2-pluto, right?

// I’ll modify the topic title a little and moved the topic to “extensions” sub-forum.

It’s only a guess, but I’m afraid you have failed to configure your user profile model for yii2-pluto. Could you show us the table schema for your user_profile table?

I followed the suggestions of the readme here

maybe I found the solution… since I use the advanced app of Yii2, I had extended the module both in the backend config and in the backend config, the ‘module’ parameter should not be pointed to my module but to the default one (‘class’ =>’ sjaakp\pluto\Module ',)
I paste below an example for the backend:
‘bootstrap’ => [‘log’,‘buser’],
‘modules’ => [
‘buser’ => [
‘class’ => ‘sjaakp\pluto\Module’, //not mine ‘common\modules\user\Module’
‘defaultRole’ => ‘support’,
‘identityClass’=> User::className(),
‘profileClass’ => UserProfile::className(),
‘passwordRegexp’ => ‘^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$’,
//… other params

p.s.
probably the model save operation was doubled!

Yeah, I think so, too.

strange though, being extended objects …