dplase3140 2013-12-27 00:12
浏览 81
已采纳

Laravel 4 - 更新子表“people”的值,同时更新父表“users”的值

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.

  • 写回答

2条回答 默认 最新

  • donglei7152 2013-12-27 01:33
    关注

    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');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计