(nolock) yii2 msdn


(Alexmetola) #1

Hi all, actually need make a querys with (nolock),

I use model and I can´t a select with (nolock).

For example:




class User extends \yii\db\ActiveRecord

{

    public static function tableName()

    {

        return 'User';

    }

}



If I make a query:


 User::findOne(['id'=>$id]);



It make:




select * from User where id=1



but I need:




select * from User (nolock) where id=1



This is possible?How?


(Mariusz) #2

got the same problem, have you found solution?


(Mzimin) #3

Just override find() method of your model:





static public function find()

{

    return parent::find()->from('User (nolock)');

}




After that you can use findOne, findAll, etc. - all these methods use find()


(Alexmetola) #4

Oh, thanks mikezimin, this work very good.

Thanks.


(Buttflattery) #5

just a question the query you are generating


select * from User (nolock) where id=1

is not a valid MySQL query are you using SQL server for databases as the query syntax above is for SQL and in MYSQL you have to do something like


SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;

SELECT * FROM TABLE_NAME ;

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ 

so does that mean that i have to use transaction block to specify nolock for MySQL select queries


(Craig London) #6

This does not work if the table is joined to other tables, invalid SQL is generated.