A
A
alestro2015-10-28 19:40:46
PHP
alestro, 2015-10-28 19:40:46

Is it possible to instantiate multiple objects via PDO::fetch_obj?

Perhaps the thought is not formulated very clearly, I will try to explain in more detail.
Now I have a certain class (described below) and a database class sharpened for mysqli, the functions are wrapped with OOP and the names are abstracted. The bottom line is this:
There are three methods in the Database_object class: find_by_sql(), find_by_id, find_all(); The next two are built on the previous one. And at the moment when I need to instantiate an instance of a class that is inherited from Database_object. I do it with these 3 methods.
For example Products::find_all() - will return all records from the products table as objects, and as you might guess, the other 2 work in a similar way. So, I want to rewrite them using PDO. But before doing this, I wonder if it is necessary to do this at all. For in PDO there is a fetch method, which, together with the fetch_obj constant, returns an instance of the std class, and if you fool around, then an instance of the called class, but it returns only one object, like find_by_id (); Perhaps there is a variant that can return all objects at once, as find_all () does;

<?php
namespace core;
abstract class Database_object{
    
    protected static $db_fields;
  
  protected static function table_fields(){
    global $db;
    $result = $db->query("SHOW COLUMNS FROM ".static::$table_name); 
    	while ($row = $db->fetch_assoc($result)){static::$db_fields[]=$row['Field'];}
  }
  
  public static function find_by_sql($sql){
    global $db;

    static::$db_fields = static::table_fields();
    $result = $db->query($sql);
    $object_array = [];
    while ($row = $db->fetch_assoc($result)){
      $object_array[] = static::instantiate($row);
    }
    return !empty($object_array) ? $object_array : false;
  }
  
  public static function count_all(){
    
  global $db;
  
  $result = $db->query("SELECT COUNT(*) FROM ".static::$table_name);
  $row = $db->fetch_assoc($result);
  return array_shift($row);
    
  }
  
  public static function find_by_sql_shift($sql=""){
  $result_array = static::find_by_sql($sql);	
  return !empty($result_array) ? array_shift($result_array) : false;		
  }

  public static function find_by_id($id=0){
    
    $result_array = static::find_by_sql("SELECT * FROM ".static::$table_name." WHERE ".static::$table_name."_id = {$id} LIMIT 1 ");
    return !empty($result_array) ? array_shift($result_array) : false;
  }
  
  public static function find_all(){
    
    return static::find_by_sql("SELECT * FROM ".static::$table_name);	
  }
  
  private static function instantiate($record){
    
    $class_name = get_called_class();
    $object = new $class_name;
    foreach($record as $attribute=>$value){
      
      if($object->has_attribute($attribute)){
      $object->$attribute = $value;}
    }
    return $object;
  }
  
  private function has_attribute($attribute) {
    return array_key_exists($attribute, $this->sanitized_attributes());
  }

  protected function attributes() { 
    $attributes = [];
    foreach(static::$db_fields as $field) {
      if(property_exists($this, $field)) {
        $attributes[$field] = $this->$field;
      }
    }
    return $attributes;
  }
  
  protected function sanitized_attributes() {
    global $db;
    $clean_attributes = [];
    foreach($this->attributes() as $key => $value){
      $clean_attributes[$key] = $db->escape($value);
    }
    return $clean_attributes;
  }
  
  public function save() {
    return isset($this->id) ? $this->update() : $this->create();
  }
  
  public function create() {
    global $db;
    $attributes = $this->sanitized_attributes();
    $sql = "INSERT INTO ".static::$table_name." (";
    $sql .= join(", ", array_keys($attributes));
    $sql .= ") VALUES ('";
    $sql .= join("', '", array_values($attributes));
    $sql .= "')";
    if($db->query($sql)) {
      $this->id = $db->insert_id();
      return true;
    } else {
      return false;
    }
  }

  public function update() {
    global $db;
    $attributes = $this->sanitized_attributes();
    $attribute_pairs = array();
    foreach($attributes as $key => $value) {
      $attribute_pairs[] = "{$key}='{$value}'";
    }
    $sql = "UPDATE ".static::$table_name." SET ";
    $sql .= join(", ", $attribute_pairs);
    $sql .= " WHERE id=". $db->escape_value($this->id);
    $db->query($sql);
    return ($db->affected_rows() == 1) ? true : false;
  }

  public function delete() {
    global $db;
    $sql = "DELETE FROM ".static::$table_name;
    $sql .= " WHERE ".static::$table_name."_id=". $db->escape_value($this->id);
    $sql .= " LIMIT 1";
    $db->query($sql);
    return ($db->affected_rows() == 1) ? true : false;
  }

}

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksey Ratnikov, 2015-10-28
@mahoho

For a PDO class object, you can use the setAttribute() method to set the default fetch type:
and after getting the entire result of the selection as follows:
$stmt->fetchAll();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question