Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MySqlObjectLoader
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 exists
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 fetch
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 fetchAll
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace UserFrosting;
4
5/* This class is responsible for retrieving generic object(s) from the database, checking for existence, etc. */
6// TODO: expand fetch functions to support arbitrary filtering, perhaps allowing for with() type clauses that support arbitary SQL
7
8abstract class MySqlObjectLoader extends MySqlDatabase implements ObjectLoaderInterface {
9    
10    protected static $_table;       // The table whose rows this class represents. Must be set in the child concrete class.   
11    
12    /**
13     * Set table and columns for this class.  Kinda hacky but I don't see any other way to do it.
14     *
15     * @param DatabaseTable $table The table information object.
16     */
17    public static function init($table){
18        static::$_table = $table;
19    }    
20    
21    /* Determine if an object exists based on the value of a given column.  Returns true if a match is found, false otherwise.
22     * @param value $value The value to find.
23     * @param string $name The name of the column to match (defaults to id)
24     * @return bool
25    */
26    public static function exists($value, $name = "id"){
27        if (static::fetch($value, $name))
28            return true;
29        else
30            return false;
31    }
32   
33    /* Fetch a result set from the table based on the value of a given column.  For non-unique columns, it will return the first entry found.  Returns false if no match is found.
34     * @param value $value The value to find.
35     * @param string $name The name of the column to match (defaults to id)
36     * @return array result set
37    */
38    public static function fetch($value, $name = "id"){
39        $db = static::connection();
40        
41        $table = static::$_table->name;
42        
43        // Check that the column name exists in the table schema.
44        if ($name != "id" && !in_array($name, static::$_table->columns))
45            throw new \Exception("The column '$name' does not exist in the table '$table'.");
46        
47        $query = "SELECT * FROM `$table` WHERE `$name` = :value LIMIT 1";
48        
49        $stmt = $db->prepare($query);
50        
51        $sqlVars[':value'] = $value;
52        
53        $stmt->execute($sqlVars);
54          
55        $results = $stmt->fetch(\PDO::FETCH_ASSOC);
56        
57        // PDO returns false if no record is found
58        return $results;
59    }
60    
61    /* Fetch all matching records from the table based on the value of a given column.  Returns empty array if no match is found.
62     * @param value $value The value to find. (defaults to null, which means return all records in the table)
63     * @param string $name The name of the column to match (defaults to null)
64     * @return array result set
65    */
66    public static function fetchAll($value = null, $name = null){
67        $db = static::connection();
68        
69        $table = static::$_table->name;
70        
71        // Check that the column name, if specified, exists in the table schema.
72        if ($name && !in_array($name, static::$_table->columns))
73            throw new \Exception("The column '$name' does not exist in the table '$table'.");
74        
75        $sqlVars = [];
76        
77        $query = "SELECT * FROM `$table`";
78        if ($name) {
79            $query .= " WHERE `$name` = :value";
80            $sqlVars[':value'] = $value;
81        }
82        
83        $stmt = $db->prepare($query);
84        
85        $stmt->execute($sqlVars);
86                  
87        // Return an array of arrays
88        $results = [];
89        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
90            $id = $row['id'];
91            $results[$id] = $row;
92        }     
93        
94        // Returns empty result set if no records found
95        return $results;
96    }
97}
98
99?>