保存后如何获取模型主键值

问题描述

保存模型后,尝试从数据库获取主键(自动增量)时出现问题。

数据库中,表格没有主键,因此我使用以下方法进行设置:

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?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)