It's a table migrated from https://github.com/lucadegasperi/oauth2-server-laravel
这是从https://github.com/lucadegasperi/oauth2-server-laravel迁移的表格
In the table oauth_clients
, the field data type of id is varchar(40), not int.
在表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();
After save(); the $client->id become '0', not the string I assigned.
保存()后; $ client-> id变为'0',而不是我指定的字符串。
That makes the following relation table save fail.
这使得以下关系表保存失败。
$endpoint = new OauthClientEndpoint(array('redirect_uri' => Input::get('redirect_uri));
$client->OauthClientEndpoint()->save($endpoint);
I checked the $client->id
: after save, it becomes 0 and I get an error including this one:
我检查了$ 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))
I manually saved an endpoint to prevent this error for now. But how do I resolve this issue?
我手动保存了一个端点,以防止此错误。但是我该如何解决这个问题呢?
Here's my model:
这是我的模特:
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');
}
}
1 个解决方案
#1
34
When you are setting your own ID and not using auto_increment be sure to add public $incrementing = false;
to that model. In your case you want:
当您设置自己的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');
}
}
This is a tiny red block in the huge Laravel documentation:
这是巨大的Laravel文档中的一个小红块:
Note: Typically, your Eloquent models will have auto-incrementing keys. However, if you wish to specify your own keys, set the incrementing property on your model to false.
注意:通常,您的Eloquent模型将具有自动递增键。但是,如果要指定自己的键,请将模型的递增属性设置为false。
#1
34
When you are setting your own ID and not using auto_increment be sure to add public $incrementing = false;
to that model. In your case you want:
当您设置自己的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');
}
}
This is a tiny red block in the huge Laravel documentation:
这是巨大的Laravel文档中的一个小红块:
Note: Typically, your Eloquent models will have auto-incrementing keys. However, if you wish to specify your own keys, set the incrementing property on your model to false.
注意:通常,您的Eloquent模型将具有自动递增键。但是,如果要指定自己的键,请将模型的递增属性设置为false。