Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
84.62% covered (success)
84.62%
11 / 13
CRAP
84.48% covered (success)
84.48%
49 / 58
CDatabaseModel
0.00% covered (danger)
0.00%
0 / 1
84.62% covered (success)
84.62%
11 / 13
16.96
84.48% covered (success)
84.48%
49 / 58
 getSource
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 findAll
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 getProperties
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 find
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 save
0.00% covered (danger)
0.00%
0 / 1
2.03
80.00% covered (success)
80.00%
4 / 5
 setProperties
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 create
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 8
 update
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
9 / 9
 delete
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 query
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 where
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 andWhere
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 execute
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
<?php
namespace Jovis\DatabaseModel;
 
/**
 * Model for databasecommunication.
 *
 */
class CDatabaseModel implements \Anax\DI\IInjectionAware
{
    use \Anax\DI\TInjectable;
 
    
    
   /**
   * Get the table name.
   *
   * @return string with the table name.
   */
  public function getSource()
  {
      return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1)));
  }  
  
  /**
   * Find and return all.
   *
   * @return array
   */
  public function findAll()
  {
      $this->db->select()
               ->from($this->getSource());
   
      $this->db->execute();
      $this->db->setFetchModeClass(__CLASS__);
      return $this->db->fetchAll();
  }
  
  /**
   * Get object properties.
   *
   * @return array with object properties.
   */
  public function getProperties()
  {
      $properties = get_object_vars($this);
      unset($properties['di']);
      unset($properties['db']);
   
      return $properties;
  }
  
  
  /**
   * Find and return specific.
   *
   * @return this
   */
  public function find($id)
  {
    $this->db->select()
             ->from($this->getSource())
             ->where("id = ?");
 
    $this->db->execute([$id]);
    return $this->db->fetchInto($this);
  }
  
  /**
   * Save current object/row.
   *
   * @param array $values key/values to save or empty to use object properties.
   *
   * @return boolean true or false if saving went okey.
   */
    public function save($values = [])
    {
    $this->setProperties($values);
    $values = $this->getProperties();
 
    if (isset($values['id'])) {
        return $this->update($values);
    } else {
        return $this->create($values);
    }
  }
  
  /**
   * Set object properties.
   *
   * @param array $properties with properties to set.
   *
   * @return void
   */
  public function setProperties($properties)
  {
      // Update object with incoming values, if any
      if (!empty($properties)) {
          foreach ($properties as $key => $val) {
              echo $key ." => " . $val . "        ";
              $this->$key = $val;
          }
      }
  }
  
  /**
   * Create new row.
   *
   * @param array $values key/values to save.
   *
   * @return boolean true or false if saving went okey.
   */
  public function create($values)
  {
      $keys   = array_keys($values);
      $values = array_values($values);
   
      $this->db->insert(
          $this->getSource(),
          $keys
      );
   
      $res = $this->db->execute($values);
   
      $this->id = $this->db->lastInsertId();
   
      return $res;
  }
  
  
  /**
   * Update row.
   *
   * @param array $values key/values to save.
   *
   * @return boolean true or false if saving went okey.
   */
  public function update($values)
  {
        $keys   = array_keys($values);
      $values = array_values($values);
   
      // Its update, remove id and use as where-clause
      unset($keys['id']);
      $values[] = $this->id;
   
      $this->db->update(
          $this->getSource(),
          $keys,
          "id = ?"
      );
   
      return $this->db->execute($values);
  }
  
  /**
   * Delete row.
   *
   * @param integer $id to delete.
   *
   * @return boolean true or false if deleting went okey.
   */
  public function delete($id)
  {
      $this->db->delete(
          $this->getSource(),
          'id = ?'
      );
   
      return $this->db->execute([$id]);
  }
  
  
  /**
   * Build a select-query.
   *
   * @param string $columns which columns to select.
   *
   * @return $this
   */
  public function query($columns = '*')
  {
      $this->db->select($columns)
               ->from($this->getSource());
   
      return $this;
  }
 
  /**
   * Build the where part.
   *
   * @param string $condition for building the where part of the query.
   *
   * @return $this
   */
  public function where($condition)
  {
      $this->db->where($condition);
   
      return $this;
  }  
    
  /**
   * Build the where part.
   *
   * @param string $condition for building the where part of the query.
   *
   * @return $this
   */
  public function andWhere($condition)
  {
      $this->db->andWhere($condition);
   
      return $this;
  }  
  
  /**
   * Execute the query built.
   *
   * @param string $query custom query.
   *
   * @return $this
   */
  public function execute($params = [])
  {
      $this->db->execute($this->db->getSQL(), $params);
      $this->db->setFetchModeClass(__CLASS__);
   
      return $this->db->fetchAll();
  }
}
?>