Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent after save id becomes 0

Tags:

It's a table migrated from https://github.com/lucadegasperi/oauth2-server-laravel

In the table oauth_clients, the field data type of id is varchar(40), not 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.

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:

(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');   } } 
like image 461
Jacky Jou Avatar asked Sep 01 '14 11:09

Jacky Jou


1 Answers

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:

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:

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.

like image 59
DutGRIFF Avatar answered Sep 21 '22 17:09

DutGRIFF