当我以这种方式使用cakephp $ this-> query("insert into [tablename]([colname])values([colvalue])插入记录时,我如何获得插入的id;
假设您使用 MySQL,您可以在插入后执行以下查询:
$array_with_id = $this->query('select last_insert_id() as id;');
但正如 kouak 提到的,插入数据的常用方法是使用 save() 方法。如果使用此方法,插入记录的 id 将自动在相应模型的 $id 属性中可用。