I'm using the framework Laravel.
I have 2 tables (Users and Persons). I want to update the values of the child table "persons" and at the same time the values of the parent table "users". I have an edit.blade.php where I made my form where I store the values from the database inside input fields.
Table Users
CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
`user_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_username` VARCHAR(45) NOT NULL,
`user_email` VARCHAR(45) NOT NULL,
`user_password` CHAR(32) NOT NULL,
`user_salt` CHAR(32) NOT NULL,
`user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_modified` TIMESTAMP NULL,
`user_deleted` TIMESTAMP NULL,
`user_lastlogin` TIMESTAMP NULL,
`user_locked` TIMESTAMP NULL,
`user_token` VARCHAR(128) NULL,
`user_confirmed` TIMESTAMP NULL,
PRIMARY KEY (`user_id`, `person_id`),
UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
INDEX `fk_users_persons1_idx` (`person_id` ASC),
CONSTRAINT `fk_users_persons1`
FOREIGN KEY (`person_id`)
REFERENCES `festival_aid`.`persons` (`person_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Table Persons
CREATE TABLE IF NOT EXISTS `festival_aid`.`persons` (
`person_id` BIGINT NOT NULL AUTO_INCREMENT,
`person_firstname` VARCHAR(45) NULL,
`person_surname` VARCHAR(45) NULL,
`person_created` TIMESTAMP NOT NULL,
`person_modified` TIMESTAMP NULL,
`person_deleted` TIMESTAMP NULL,
PRIMARY KEY (`person_id`))
ENGINE = InnoDB;
Edit action
public function edit($person_id)
{
$person = Person::find($person_id);
//$person = Person::with('user')->orderBy('person_id');
return View::make('persons.edit')
->with('person', $person);
}
Update action
public function update($person_id)
{
$rules = array();
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('persons/' . $person_id . '/edit')
->withErrors($validator);
} else {
//$person = Person::with('user')->orderBy('person_id');
$person = Person::find($person_id);
$person->person_firstname = Input::get('person_firstname');
$person->person_surname = Input::get('person_surname');
//$person->user->user_username = Input::get('user_username');
//$person->user->user_surname = Input::get('user_surname');
//$person->user->user_email = Input::get('user_email');
$person->save();
Session::flash('message', 'Successfully updated user!');
return Redirect::to('persons');
}
}
Edit view
<legend>Edit {{ $person->person_firstname }}</legend>
{{ HTML::ul($errors->all()) }}
{{ Form::model($person->user, array('route' => array('persons.update', $person->person_id), 'method' => 'PUT')) }}
<div class="form-group">
{{ Form::label('firstname', 'Firstname') }}
{{ Form::text('person_firstname', $value = $person->person_firstname, array('class'=>'form-control', 'placeholder' => 'Firstname')) }}
</div>
<div class="form-group">
{{ Form::label('surname', 'Surname') }}
{{ Form::text('person_surname', $value = $person->person_surname, array('class'=>'form-control', 'placeholder' => 'Surname')) }}
</div>
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('user_username', $value = $person->user->user_username, array('class'=>'form-control', 'placeholder' => 'Username')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('user_email', $value = $person->user->user_email, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('Password') }}
{{ Form::password('user_password', array('class'=>'form-control', 'placeholder' => 'Password')) }}
</div>
{{ Form::submit('Edit the User!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
@stop
User migration
Schema::table('users', function(Blueprint $table)
{
$table->increments('user_id');
$table->string('user_email');
$table->timestamp('user_created');
$table->timestamp('user_modified');
$table->timestamp('user_deleted');
$table->timestamp('user_lastlogin');
$table->timestamp('user_locked');
$table->foreign('person_id')
->references('id')->on('persons')
->onDelete('cascade');
});
Person migration
public function up()
{
Schema::table('persons', function(Blueprint $table)
{
$table->increments('person_id');
$table->string('person_firstname');
$table->string('person_surname');
});
}
Model User
class User extends Eloquent {
protected $primaryKey = 'user_id';
public function persons()
{
return $this->hasOne('Person');
}
public $timestamps = false;
}
Model Person
class Person extends Eloquent {
protected $table = 'persons';
protected $primaryKey = 'person_id';
public function users()
{
return $this->belongsTo('User');
}
public $timestamps = false;
}
My problem is that the update action only updates the values from the persons table and not from the users table.
I found a solution:
public function update($person_id) {
$rules = array();
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('persons/' . $person_id . '/edit')
->withErrors($validator);
} else {
$user = User::find($person_id); // <= ADD THIS
$person = Person::find($person_id);
$person->person_firstname = Input::get('person_firstname');
$person->person_surname = Input::get('person_surname');
$user->user_username = Input::get('user_username'); // <= ADD THIS
$user->user_email = Input::get('user_email'); // <= ADD THIS
$person->save();
$user->save();
Session::flash('message', 'Successfully updated user!');
return Redirect::to('persons');
}
}