1

我有一个简单(我想这很容易)的问题。我创建了一个名为 Player.php 的新模型。它看起来像这样:

    <?php

    class Player extends Eloquent {


        protected $table = 'players';

        protected $fillable = array('char_name', 'sex');
        public $timestamps = false;

}

我已经在我的控制器中使用了它:

public function post_character()
{
    $input = Input::all();

    $rules = array(
      'char_name' => 'required|unique:players,name|min:4',
      'sex' => 'required'
      );

    $validation = Validator::make($input, $rules);

    if ($validation->fails()) {
      return Redirect::to('create_character')->withErrors($validation);
    } else {

      $player = new Player;
      $player->name = $input['char_name'];
      $player->sex = $input['sex'];
      $player->save;

      return Redirect::to('managment')->with('success', 'Your character has been created.');
    }
}

这是我的表格:

@extends('base')
@section('main')

<div class="doc-content-box">
    <legend>Create a character</legend>
    {{ Form::open(array('url' => 'create_character')) }}

        <div class="control-group">
  {{ Form::label('char_name', 'Character name') }}
  <div class="controls">
    {{ Form::text('char_name', null, array('placeholder' => 'Character name goes here..')) }}
  </div>
</div>

   <div class="control-group">
      <div class="controls">
{{ Form::select('sex', array('1' => 'Male', '2' => 'Female'), '1'); }}
 </div>
</div>

<div class="form-actions">
  {{ Form::submit('Create a character', array('class' => 'btn btn-success')) }}
  {{ Form::button('Help', array('class' => 'btn btn-default')) }}
</div>
    {{ Form::close() }}
</div>

@endsection

现在,这是我的错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`thefs`.`players`, CONSTRAINT `players_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE) (SQL: insert into `players` (`name`, `sex`) values (?, ?)) (Bindings: array ( 0 => 'Malx', 1 => '1', ))

另外这里是我的数据库表“玩家”的屏幕截图:

http://3.imgland.net/lL6EIv.png

4

3 回答 3

1

好吧,错误表明您对玩家表有限制。在这里创建用户:

  $player = new User;
  $player->name = $input['char_name'];
  $player->sex = $input['sex'];
  $player->save;

但是你没有设置account_id。您可以在 phpmyadmin 中默认将此字段设置为 NULL 或删除外键约束或设置正确的 account_id

顺便说一句,你可以写:

$player = Player::create(array(
    'name' => ...
));

而不是创建新对象然后保存它

于 2013-07-23T14:37:38.860 回答
0

您的模型中似乎存在帐户和玩家之间的关系。您必须在控制器中建立这种关系。

于 2013-07-23T14:37:08.267 回答
0

这可能是因为您没有在 Eloquent 模型中定义用户和玩家之间的关系。这是您可以做到的一种常见方法(请注意,我将您的“帐户”模型替换为“用户”[因此,您的帐户表将成为用户表,玩家表将具有 user_id 而不是 account_id 的外键] ,因为它与 Laravel 的默认身份验证机制配合得更好)。

//user.php
class User extends Eloquent {
    //Define relation to players
    public function players() {
        return $this->hasMany('Player');
    }
}


//player.php
class Player extends Eloquent {
    //Your other player model code

    //Define relation to user 
    public function user() {
        return $this->belongsTo('User');
    }
}

现在在创建新播放器时:

$player = new Player();
$player->user_id = Auth::user()->id;
$player->name = $input['char_name'];
$player->sex = $input['sex'];
$player->save();

这假设用户 id 来自任何登录的用户。

于 2013-07-23T19:54:59.040 回答