【问题标题】:Laravel Eloquent after save id becomes 0保存 id 变为 0 后 Laravel Eloquent
【发布时间】:2014-10-25 14:38:13
【问题描述】:

是从https://github.com/lucadegasperi/oauth2-server-laravel迁移过来的表

oauth_clients中,id的字段数据类型是varchar(40),不是int。

$name = Input::get('name');
$id = str_random(40);
$secret = str_random(40);

$client = new oauthClient;
$client->name = $name;
$client->id = $id;
$client->secret = $secret;
$client->save();

保存后(); $client->id 变为 '0',而不是我分配的字符串。

这使得下面的关系表保存失败。

$endpoint = new OauthClientEndpoint(array('redirect_uri' => Input::get('redirect_uri));
$client->OauthClientEndpoint()->save($endpoint);

我检查了$client->id:保存后,它变成0,我收到一个错误,包括这个:

(SQL: insert into `oauth_client_endpoints` (`redirect_uri`, `client_id`, `updated_at`, `created_at`) values (http://www.xxxxx.com, 0, 2014-09-01 11:10:16, 2014-09-01 11:10:16))

我暂时手动保存了一个端点以防止出现此错误。但是我该如何解决这个问题呢?

这是我的模型:

class OauthClient extends Eloquent {

  protected $table = 'oauth_clients';

  public function OauthClientEndpoint(){
    return $this->hasOne('OauthClientEndpoint', 'client_id', 'id');
  }

}

class OauthClientEndpoint extends Eloquent {

  protected $table = 'oauth_client_endpoints';
  protected $fillable = array('redirect_uri');

  public function OauthClient(){
    return $this->belongsTo('OauthClient', 'client_id', 'id');
  }

}

class CreateOauthClientsTable extends Migration {
  public function up() {
    Schema::create('oauth_clients', function (Blueprint $table) {
      $table->string('id', 40);
      $table->string('secret', 40);
      $table->string('name');
      $table->timestamps();

      $table->unique('id');
      $table->unique(array('id', 'secret'));
    });
  }

  public function down() {
    Schema::drop('oauth_clients');
  }
}

class CreateOauthClientEndpointsTable extends Migration {
  public function up() {
    Schema::create('oauth_client_endpoints', function (Blueprint $table) {
      $table->increments('id');
      $table->string('client_id', 40);
      $table->string('redirect_uri');

      $table->timestamps();

      $table->foreign('client_id')
        ->references('id')->on('oauth_clients')
        ->onDelete('cascade')
        ->onUpdate('cascade');

    });
  }

  public function down() {
    Schema::table('oauth_client_endpoints', function ($table) {
      $table->dropForeign('oauth_client_endpoints_client_id_foreign');
    });

    Schema::drop('oauth_client_endpoints');
  }
}

【问题讨论】:

  • 你能发布你的 oauthClient 模型吗?
  • 请提供您创建的客户端模型
  • 添加了我创建的模型。
  • 我发现添加 public $incrementing = false;进入模型 OauthClient 然后它解决了。

标签: php laravel eloquent save


【解决方案1】:

当您设置自己的 ID 并且不使用 auto_increment 时,请务必将 public $incrementing = false; 添加到该模型。在你的情况下,你想要:

class OauthClient extends Eloquent {

  public $incrementing = false;
  protected $table = 'oauth_clients';

  public function OauthClientEndpoint(){
    return $this->hasOne('OauthClientEndpoint', 'client_id', 'id');
  }

}

这是巨大的 Laravel 文档中的一个小红块:

注意:通常,您的 Eloquent 模型将具有自动递增的键。但是,如果您希望指定自己的键,请将模型上的增量属性设置为 false。

【讨论】:

  • 非常感谢!我只花了 2 个小时思考我做错了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多