我有一個多到許多關係的記錄,所以實施需要創建新表有兩個主鍵獲取錯誤,,當試圖插入具有複合主鍵
這裏的新表:
public function up()
{
Schema::create('friendship', function(Blueprint $table)
{
$table->integer('user1_id')->index('fk_user_has_user_user1_idx');
$table->integer('user2_id')->index('fk_user_has_user_user2_idx');
$table->boolean('state')->nullable();
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->primary(['user1_id','user2_id']);
});
}
但是當嘗試使用此功能插入數據: //插入新友誼大到數據庫
public function InsertFriendShip($User1Id, $User2Id, $State)
{
$friend = new FriendShip();
$friend->User1_Id = $User1Id;
$friend->User2_Id = $User2Id;
$friend->State = $State;
// save new FriendShip to the database
if(!$friend->save())
return 'created';
}
確定它在數據庫中創建新的記錄,但雖然它返回此錯誤 PDO :: lastInsertId()預計參數1是字符串數組給定
你的意思是創建一個新的表與複合主鍵(由2個字段組成) – Veverke 2015-02-10 17:01:14
是的,我的意思是 – 2015-02-10 17:03:35
首先,從你得到的錯誤,它似乎並不是插入現有主鍵的問題,而不是類型不兼容/不一致問題。你需要確定你傳遞的是「User1Id」是一個字符串(我不熟悉PHP,不知道「給定的數組」是什麼意思) – Veverke 2015-02-10 17:07:47