📜  Yii-数据访问对象

📅  最后修改于: 2020-10-16 07:24:05             🧑  作者: Mango


要执行SQL查询,您应该遵循以下步骤-

  • 使用SQL查询创建yii \ db \ Command
  • 绑定参数(不需要)
  • 执行命令。

第1步-创建一个在SiteController称为actionTestDb函数。

public function actionTestDb(){
   // return a set of rows. each row is an associative array of column names and values.
   // an empty array is returned if the query returned no results
   $users = Yii::$app->db->createCommand('SELECT * FROM user LIMIT 5')
      ->queryAll();
   var_dump($users);
   // return a single row (the first row)
   // false is returned if the query has no result
   $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE id=1')
      ->queryOne();
   var_dump($user);
   // return a single column (the first column)
   // an empty array is returned if the query returned no results
   $userName = Yii::$app->db->createCommand('SELECT name FROM user')
      ->queryColumn();
   var_dump($userName);
   // return a scalar value
   // false is returned if the query has no result
   $count = Yii::$app->db->createCommand('SELECT COUNT(*) FROM user')
      ->queryScalar();
   var_dump($count);
}

上面的示例显示了从数据库获取数据的各种方式。

步骤2-转到地址http:// localhost:8080 / index.php?r = site / test-db ,您将看到以下输出。

创建actionTestDb输出

创建一个SQL命令

要创建带有参数的SQL命令,应始终使用绑定参数的方法来防止SQL注入。

步骤1-以这种方式修改actionTestDb方法。

public function actionTestDb() {
   $firstUser = Yii::$app->db->createCommand('SELECT * FROM user WHERE id = :id')
      ->bindValue(':id', 1)
      ->queryOne();
   var_dump($firstUser);
   $params = [':id' => 2, ':name' => 'User2'];
   $secondUser = Yii::$app->db->createCommand('SELECT * FROM user WHERE
      id = :id AND name = :name')
      ->bindValues($params)
      ->queryOne();
   var_dump($secondUser);
      //another approach
   $params = [':id' => 3, ':name' => 'User3'];
   $thirdUser = Yii::$app->db->createCommand('SELECT * FROM user WHERE
      id = :id AND name = :name', $params)
      ->queryOne();
   var_dump($thirdUser);
}

在上面的代码中-

  • bindValue() -绑定单个参数值。

  • bindValues() -绑定多个参数值。

步骤2-如果转到地址http:// localhost:8080 / index.php?r = site / test-db ,您将看到以下输出。

修改后的actionTestDb输出

插入,更新和删除查询

对于INSERT,UPDATE和DELETE查询,可以调用insert(),update()和delete()方法。

步骤1-以这种方式修改actionTestDb方法。

public function actionTestDb() {
   public function actionTestDb(){
      // INSERT (table name, column values)
      Yii::$app->db->createCommand()->insert('user', [
         'name' => 'My New User',
         'email' => 'mynewuser@gmail.com',
      ])->execute();
      $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE name = :name')
         ->bindValue(':name', 'My New User')
         ->queryOne();
      var_dump($user);
      // UPDATE (table name, column values, condition)
      Yii::$app->db->createCommand()->update('user', ['name' => 'My New User
         Updated'], 'name = "My New User"')->execute();
      $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE name = :name')
         ->bindValue(':name', 'My New User Updated')
         ->queryOne();
      var_dump($user);
      // DELETE (table name, condition)
      Yii::$app->db->createCommand()->delete('user', 'name = "My New User
         Updated"')->execute();
      $user = Yii::$app->db->createCommand('SELECT * FROM user WHERE name = :name')
         ->bindValue(':name', 'My New User Updated')
         ->queryOne();
      var_dump($user);
   }
}

步骤2-在Web浏览器的地址栏中输入URL http:// localhost:8080 / index.php?r = site / test-db ,您将看到以下输出。

插入更新删除查询示例