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

时间:2020-10-27 14:19:56

标签: yii2 yii2-advanced-app

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

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

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?

0 个答案:

没有答案
相关问题