简体   繁体   English

如何在yii createCommand中获取ID新记录?

[英]How to get id new record in yii createCommand?

How to get id new record in yii createCommand? 如何在yii createCommand中获取ID新记录? i try this 我尝试这个

$insert = Yii::app()->db->createCommand("INSERT INTO {$tableName}(the_geom) values(ST_GeomFromText('{$cordinate}'));")->query();

and i add code 我添加代码

$insert_id = Yii::app()->db->getLastInsertID($insert);

the result is an error, please help me.. 结果是错误,请帮助我。

Try this sure you get the last id 尝试确保您得到了最后一个ID

If using postgresql 如果使用PostgreSQL

$lastID = Yii::app()->db->getLastInsertID('table_sequence');

If using mysql 如果使用mysql

$lastID = Yii::app()->db->getLastInsertID();

since getLastInsertID is accessor method you also call like this too 由于getLastInsertID是访问器方法,因此您也要这样调用

$lastID = Yii::app()->db->lastInsertID

May be this discussion helpful to you. 此讨论可能对您有所帮助。 please click me!! 点击我!

for official documention click me!! 官方文件请点击我!

And In this thread there is a clear explanation of issue occurring during usage of getter/accessor and setter/mutator method as property in conditional check click me!! 并且在此线程中,有条理清楚地解释了在使用条件获取器中的getter / accessor和setter / mutator方法作为属性期间发生的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM