Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eloquent cannot save models with composite primary keys #6245

Closed
hzxie opened this issue Oct 27, 2014 · 3 comments
Closed

Eloquent cannot save models with composite primary keys #6245

hzxie opened this issue Oct 27, 2014 · 3 comments

Comments

@hzxie
Copy link

hzxie commented Oct 27, 2014

When defining composite primary keys then calling save on an instanced model, an exception is thrown.

ErrorException (E_UNKNOWN) 
PDO::lastInsertId() expects parameter 1 to be string, array given

And here's the declaration of Model

class ActivityAttendance extends Eloquent {
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'activity_attendance';

    /**
     * The primary key of the table.
     * 
     * @var string
     */
    protected $primaryKey = array('activity_id', 'username');

    /**
     * The attributes of the model.
     * 
     * @var array
     */
    protected $guarded = array('activity_id', 'username', 'guests');

    /**
     * Disabled the `update_at` field in this table.
     * 
     * @var boolean
     */
    public $timestamps = false;
}

Here's the code to create new record in Controller class.

$attendance                 = new ActivityAttendance;
$attendance->activity_id    = $activityId;
$attendance->username       = $username;
$attendance->save();
@hzxie
Copy link
Author

hzxie commented Oct 27, 2014

The issue is relative to issue #5248

@Marwelln
Copy link
Contributor

This issue is well known. Eloquent doesn't support composite primary keys.

@ghost
Copy link

ghost commented Jun 12, 2015

Its works!!!
I ignored the model of the relationship table and set "belongsToMany" in the two tables with a little details: set the two foreign key of two tables and set the name of the relationship table. The whole foreign keys will works like a composite key.

Ex:
//--------- Model "Table1": ------------//
class Table1 extends Model {
...
public function table2()
{
return $this->belongsToMany('App\Table2', 'table_relashionship', 'id_table2', 'id_table1');
}
}

//--------- Model "Table2": ------------//
class Table2 extends Model {
...
public function table1()
{
return $this->belongsToMany('App\Table1', 'table_relashionship', 'id_table1', 'id_table2');
}
}

Now the Eloquent undestand that you have a 'table_relashionship' in your database with columns 'id_table1' and 'id_table2' and you can access the data with the comand:
$table1 = Table2::find(1);
dd($table1->table2)

Its solve my problem!

bartero referenced this issue in GrupaZero/cms Sep 14, 2015
Added working models with repo and complete unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants