保存模型后,尝试从数据库获取主键(自动增量)时出现问题。
在数据库中,表格没有主键,因此我使用以下方法进行设置:
public static function primaryKey(){
return ['id_cod'];
}
我将其从模型规则中删除。
当我保存模型时,所有数据都保存在包含id_cod
的数据库中,但是,如果我尝试访问此属性,它将为空。
$evento = new Evento();
//populate the model with some data
//..... eg: $evento->name = 'name';
//save model
$evento->save();
$evento->refresh();
echo Yii::$app->db->getLastInsertID(); // gives me the correct id_cod but i think it's not the correct way
echo $evento->id_cod;//gives me null
echo $evento->name;//gives me name
保存模型后如何获取id_cod?