当定义复合主键然后在实例化模型上调用 save 时,会引发异常。
ErrorException (E_UNKNOWN)
PDO::lastInsertId() expects parameter 1 to be string, array given
错误发生在第32行
$id = $query->getConnection()->getPdo()->lastInsertId($sequence);
这是模型的声明
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;
}
这是在 Controller 类中创建新记录的代码。
$attendance = new ActivityAttendance;
$attendance->activity_id = $activityId;
$attendance->username = $username;
$attendance->save();
我假设这是一个数据透视表,在这种情况下,您不应该直接使用数据透视表。与任一工作Activity
or Attendance
使用以下方法的模型belongsToMany()
更新数据透视表,sync()
, attach()
, detach()
, etc...
如果由于某种原因这是不可能的,因为您的数据透视表还包含其他地方的键,您应该删除当前的主键,添加一个id
主键列自增并添加唯一索引username
, and activity_id
.
你也许也可以像这样保存它......可能值得一试。
$attendance = ActivityAttendance::create(
'activity_id' => $activityId,
'username' => $username
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)