M
M
Max Ba2019-06-25 15:37:24
PHP
Max Ba, 2019-06-25 15:37:24

Is the DB class well written?

Please point out gross errors in the code. I plan to use this class. I don't want to suddenly run into trouble. Leaving singleton.
PS. it all started with the phrase "no work with the database should be in the model." I also read a lot about SOLID)))
In general, did you write the code correctly?

class DB
{
  protected $db;

  public function __construct(){
    //require_once 'config_db.php';
    try{
      $this->db = new PDO('mysql:host=localhost;dbname=test','root','');  
    }catch(PDOException $e){
      die('DB ERROR');
    }
  }

  private function query($query, array $params = array()){
    $stmt = $this->db->prepare($query);
    if(!empty($params)){
      foreach($params as $k => $v){
        if(is_int($v)){
          $stmt->bindValue(':'.$k, $v, PDO::PARAM_INT);
        }else{
          $stmt->bindValue(':'.$k, $v, PDO::PARAM_STR);
        }
      }
    }
    $stmt->execute();
    return $stmt;
  }

  public function getRows($query, array $params = array()){
    return $this->query($query, $params)->fetchAll(PDO::FETCH_ASSOC);
  }

  public function getColumn($query, array $params = array()){
    return $this->query($query, $params)->fetchColumn();
  }

  public function lastInsertId(){
    return $this->db->lastInsertId();
  }
}


class Car
{
  protected $db;

  public function __construct(DB $db){
    $this->db = $db;
  }

  public function getAll(){
    $query = "SELECT `id`, `name` FROM `cars`";
    return $this->db->getRows($query);
  }

  public function getById(int $id){
    $query = "SELECT `id`, `name` FROM `cars` WHERE `id` = :id";
    return $this->db->getRows($query, array('id' => $id));
  }
}



$db = new DB();

$car = new Car($db);

var_dump($car->getAll());

var_dump($car->getById(1));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2019-06-25
@phpcoder81

die('DB ERROR');
Not right away! The class should not stop the application if an error occurs. It should throw an exception, and what to do next should be decided by the client code.
In what sense is it written? Where is that written?
Will you create a separate connection to the database for each request? There is generally a limit on the number of open.
I didn't notice much difference between working with mysqli and working with your class. Bind parameter encapsulation query method only? So inherit from the mysqli class and write a convenient method for this.
And why don't you take some kind of ready-made wrapper? Thankfully there are thousands of them!

P
profesor08, 2019-06-25
@profesor08

To get the column names and data row by row, you need to run two identical queries. Asking why? You can remove all the code, and it will become easier to write queries.
If you want on classes, then you need one class to create queries to the database, the second to process the result.

class Sql {
  public function query() {
    ....
    return new SqlResult($stmt);
  }
}

class SqlResult {
  public function __constructor($stmt) {
    $this->stmt = $stmt;
  }

  public function getCollumns() {
    return $this->stmt->fetchColumn();
  }

  public function getRows() {
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question