【发布时间】:2013-12-27 00:12:00
【问题描述】:
我正在使用框架 Laravel。
我有 2 个表(用户和人员)。我想更新子表“persons”的值,同时更新父表“users”的值。我有一个 edit.blade.php,我在其中创建了我的表单,我将数据库中的值存储在输入字段中。
表用户
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;
桌人
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;
编辑操作
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);
}
更新操作
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');
}
}
编辑视图
<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
用户迁移
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');
});
人员迁移
public function up()
{
Schema::table('persons', function(Blueprint $table)
{
$table->increments('person_id');
$table->string('person_firstname');
$table->string('person_surname');
});
}
模型用户
class User extends Eloquent {
protected $primaryKey = 'user_id';
public function persons()
{
return $this->hasOne('Person');
}
public $timestamps = false;
}
模范人物
class Person extends Eloquent {
protected $table = 'persons';
protected $primaryKey = 'person_id';
public function users()
{
return $this->belongsTo('User');
}
public $timestamps = false;
}
我的问题是更新操作只更新了 person 表中的值,而不是 users 表中的值。
【问题讨论】:
标签: php laravel-4 parent eloquent