protected function beforeSave()
{
// Detect nullable foreign keys and replace "" with null (to fix html dropdowns breaking contraints)
foreach ($this->tableSchema->foreignKeys as $field => $stuff) {
if ($this->tableSchema->columns[$field]->allowNull && !$this->{$field}) {
$this->{$field} = null;
}
}
return parent::beforeSave();
}