Yii2 Query Builder provides an elegant and safe way to build and execute database queries using a fluent interface. It helps avoid raw SQL queries while still allowing flexibility.
To use Query Builder, call Yii::$app->db->createCommand() with appropriate query methods.
Fetching all rows from the master_state table:
$states = (new \yii\db\Query())
->select(['state_code', 'state_name'])
->from('master_state')
->where(['status' => 1])
->all();Fetching a single row:
$state = (new \yii\db\Query())
->select(['state_code', 'state_name'])
->from('master_state')
->where(['state_code' => 101])
->one();Fetching a single column:
$stateNames = (new \yii\db\Query())
->select('state_name')
->from('master_state')
->column();Inserting a new record into master_state:
Yii::$app->db->createCommand()
->insert('master_state', [
'state_code' => 105,
'state_name' => 'New State',
'status' => 1
])
->execute();Updating the name of a state:
Yii::$app->db->createCommand()
->update('master_state', ['state_name' => 'Updated State'], ['state_code' => 105])
->execute();Deleting a record from master_state:
Yii::$app->db->createCommand()
->delete('master_state', ['state_code' => 105])
->execute();Sorting results, limiting, and using joins:
$states = (new \yii\db\Query())
->select(['s.state_code', 's.state_name', 'z.zone_name'])
->from('master_state s')
->leftJoin('master_zone z', 's.zone_code = z.zone_code')
->where(['s.status' => 1])
->orderBy(['s.state_name' => SORT_ASC])
->limit(10)
->all();DAO (Data Access Object) allows executing raw SQL queries directly. It is useful when dealing with complex queries.
$rows = Yii::$app->db->createCommand('SELECT * FROM master_state WHERE status = :status')
->bindValue(':status', 1)
->queryAll();Fetching a single row:
$row = Yii::$app->db->createCommand('SELECT * FROM master_state WHERE state_code = :code')
->bindValue(':code', 101)
->queryOne();Fetching a single column:
$stateNames = Yii::$app->db->createCommand('SELECT state_name FROM master_state')
->queryColumn();Fetching a scalar value:
$count = Yii::$app->db->createCommand('SELECT COUNT(*) FROM master_state')
->queryScalar();Yii::$app->db->createCommand('INSERT INTO master_state (state_code, state_name, status) VALUES (:code, :name, :status)')
->bindValues([':code' => 106, ':name' => 'Test State', ':status' => 1])
->execute();Yii::$app->db->createCommand('UPDATE master_state SET state_name = :name WHERE state_code = :code')
->bindValues([':name' => 'Modified State', ':code' => 106])
->execute();Yii::$app->db->createCommand('DELETE FROM master_state WHERE state_code = :code')
->bindValue(':code', 106)
->execute();$states = (new \yii\db\Query())
->select(['state_code', 'state_name'])
->from('master_state')
->where(['status' => 1])
->orderBy(['state_name' => SORT_ASC])
->all();Yii::$app->db->createCommand('INSERT INTO master_state (state_code, state_name, status) VALUES (:code, :name, :status)')
->bindValues([':code' => 110, ':name' => 'Real State', ':status' => 1])
->execute();Yii::$app->db->createCommand()
->update('master_state', ['state_name' => 'Updated Real State'], ['state_code' => 110])
->execute();Yii::$app->db->createCommand('DELETE FROM master_state WHERE state_code = :code')
->bindValue(':code', 110)
->execute();
Sign in to join the discussion and post comments.
Sign in