Exemple #1
0
 /**
  * Executes an SQL query and returns the inserted record identifier or the mysqli result object
  *
  * - if $class_name is set and $query is a 'SELECT', returned value will be an object[]
  * - if $query is a 'SELECT' without $class_name, then returned value will be a mysqli_result
  * - if $query is an 'INSERT' returned value will be the last insert id
  * - other cases : returned value make no sense : do not use it ! (may be null or last insert id)
  *
  * @param $query string
  * @param $class_name string if set, the result will be object[] with read data
  * @return integer|mysqli_result|object[]
  */
 public function query($query, $class_name = null)
 {
     if ($query) {
         $result = $this->connection->query($query);
         if (isset($class_name)) {
             if ($class_name === AS_ARRAY) {
                 $objects = [];
                 while ($element = $result->fetch_assoc()) {
                     if (isset($element['id'])) {
                         $objects[$element['id']] = $element;
                     } else {
                         $objects[] = $element;
                     }
                 }
                 $result->free();
             } else {
                 $class_name = Builder::className($class_name);
                 $objects = [];
                 while ($object = $result->fetch_object($class_name)) {
                     if (isset($object->id)) {
                         $objects[$object->id] = $object;
                     } else {
                         $objects[] = $object;
                     }
                 }
                 $result->free();
                 $this->afterReadMultiple($objects);
             }
         } else {
             $objects = $this->connection->isSelect($query) ? $result : $this->connection->insert_id;
         }
     } else {
         $objects = null;
     }
     return $objects;
 }