Reputation: 133
I'm trying to update existing entries in my MySQL database with eloquent's 'save()' method, but it keeps giving me the following error:
Illuminate \ Database \ QueryException
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
the save() method is supposed to detect the record via primary key and update the record instead of trying to insert it right? what am i missing here?
here is the beginning of my user model:
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $primaryKey = 'objectguid';
public $incrementing = false;
this is how i created the users table: (laravel migration)
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->string('objectguid')->primary();
$table->string('username');
$table->string('email');
$table->string('firstname');
$table->string('lastname');
$table->string('displayname');
$table->string('company');
$table->string('department');
$table->string('title');
$table->integer('phone');
$table->date('start_date');
$table->timestamps();
});
}
And here is my controller method:
public function pull()
{
$users = $this->ldap->search([
'objectguid', 'samaccountname', 'mail', 'title',
'sn', 'givenname', 'displayname', 'department',
'company', 'telephonenumber', 'whencreated'
])->get();
foreach($users as $user)
{
$user->save();
}
}
what is really throwing me off is if i use the User::find(objectguidofrecordhere)
it works as expected and finds the record no problem.
Upvotes: 1
Views: 3428
Reputation: 133
Here is what i did to get around this problem. Since I'm not initially retrieving the model from the MySQL database using Eloquent, it is not changing the exists
flag in the User
model's instance, therefore the save()
method still believes the model does not exist in the database.
I added the following method to my User
model to work around this problem.
public function checkExistence()
{
if( ! is_null(User::find($this->objectguid)))
{
$this->exists = true;
return true;
}
return false;
}
And changed my controller's method to the following:
public function pull()
{
$users = $this->ldap->search([
'objectguid', 'samaccountname', 'mail', 'title',
'sn', 'givenname', 'displayname', 'department',
'company', 'telephonenumber', 'whencreated'
])->get();
foreach($users as $user)
{
$user->checkExistence();
$user->save();
}
return View::make('ldap.display', ['users' => $users]);
}
Again, thank you everyone for your input and helping me come to this solution.
Upvotes: 1
Reputation: 91213
the save() method is supposed to detect the record via primary key and update the record instead of trying to insert it right?
I do not believe that is how the save method works. You need to query for a model to determine its existence or not, then act accordingly:
$user = User::find(1234);
if ( is_null($user) ) {
// User doesn't exist
$user = new User();
}
$user->someAttribute = 'taco';
$user->save();
If the user is null
, then it doesn't exist in the database and you have to create a new object. Then update whatever attributes you want. Then save it.
As far as I know, save does not automatically determine if the objects exists already for you. I could be wrong if this is some hidden undocumented Eloquent feature though.
Upvotes: 1