2014-09-01 13 views
13

यह एक मेज https://github.com/lucadegasperi/oauth2-server-laravelLaravel सुवक्ता के बाद बचाने आईडी हो जाता है 0

से पलायन कर तालिका oauth_clients में है, आईडी के क्षेत्र डेटा प्रकार varchar (40) है, इंट नहीं।

$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(); 

सहेजने के बाद(); $ क्लाइंट-> आईडी '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'); 
    } 
} 
+0

क्या आप अपना ऑउथ क्लाइंट मॉडल पोस्ट कर सकते हैं? – JofryHS

+0

कृपया आपके द्वारा बनाए गए क्लाइंट मॉडल को – lagbox

+0

बनाए गए मॉडल प्रदान करें। –

उत्तर

27

जब आप अपनी खुद की आईडी की स्थापना कर रहे हैं और उपयोग नहीं कर 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 दस्तावेज में एक छोटे से लाल ब्लॉक है। हालांकि, अगर आप अपनी खुद की चाबियाँ निर्दिष्ट करना चाहते हैं, तो अपने मॉडल पर बढ़ती हुई संपत्ति को गलत पर सेट करें।

+1

आपको बहुत बहुत धन्यवाद! मैंने बस 2 घंटे बिताए कि मैं क्या गलत कर रहा था। – Notflip

संबंधित मुद्दे