How to write locking table in AR?

Hi all,

I would like to add table lock in my script, may i know active record have this function. I have check the forum and all is using DAO method. Any one can help me?


you can write simple sql lock table with

$connection=Yii::app()->db;   // assuming you have configured a "db" connection

$command=$connection->createCommand('LOCK TABLE TABLENAME;');


more to read mysq

Can i use AR to write the table locking? because all the page was using AR to write in the model.

you can make in active record. Like that

class Post extends ActiveRecord {


    public function beforeSave() {

        Yii::app()->db->createCommand('LOCK TABLE '.$this->getTableName().' WRITE;')->execute(); 

        return parent::beforeSave();


    public function afterSave() {

        Yii::app()->db->createCommand('UNLOCK TABLES;')->execute(); 





Thank R.K, it was really help.