Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created September 6, 2011 15:20
Show Gist options
  • Save agmcleod/1197833 to your computer and use it in GitHub Desktop.
Save agmcleod/1197833 to your computer and use it in GitHub Desktop.
Model base class
<?php
class Model
{
private $id;
private $data = Array();
private $fieldNames = Array();
public static function find($id)
{
$tn = self::getTableName();
$conn = createDefaultConnection();
// setup dummy object to get field names
$cName = get_called_class();
$tempObj = new $cName;
$stmt = $conn->prepare("SELECT ". $tempObj->getFieldNamesAsString() ." FROM $tn WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute() or die('Query failed');
$stmt->bind_result($id, $name);
$stmt->store_result();
$object;
while($stmt->fetch())
{
$object = new $cName;
$object->id = $id;
$object->name = $name;
}
$stmt->close();
$conn->close();
return $object;
}
public function getFieldNamesAsString()
{
return implode(", ", $this->getFieldNames());
}
/**
* Do not use, implement on extended model
*/
public function getFieldNames()
{
return $this->fieldNames;
}
/**
* Retrieves all records based on a where condition
*
* @param conditions - Array of conditions. The first value is expected to the be the WHERE string, with ? for values. The rest of the
* elements are expected to be the values to go in those question marks. Example: getAllWhere(Array("name = ?", "Zerg"))
* @param $types - An array containing appropriate characters identifying the object types of each value in the array. So if
* you are passing array('WHERE id=? AND name=?, 1, 'Aaron'), then $types would equal Array("i", "s"). See: http://php.net/manual/en/mysqli-stmt.bind-param.php
* @return Array of objects
**/
public static function getAllWhere($conditions = Array(), $types = Array())
{
$tn = self::getTableName();
$where = '';
$numOfConditions = sizeof($conditions);
if($numOfConditions > 0)
{
$where = 'WHERE ' . $conditions[0];
}
// create temp object to get field names
$cName = get_called_class();
$tempObj = new $cName;
$stmtString = "SELECT ". $tempObj->getFieldNamesAsString() ." FROM $tn $where";
// begin building statement
$conn = createDefaultConnection();
$stmt = $conn->prepare($stmtString);
for($i = 1; $i < $numOfConditions; $i++)
{
$stmt->bind_param($types[$i-1], $conditions[$i]);
}
$stmt->execute() or die('Query failed');
$meta = $stmt->result_metadata();
// initialise some empty arrays
$fields = array();
// This is the tricky bit dynamically creating an array of variables to use
// to bind the results
$i = 0;
while ($field = $meta->fetch_field()) {
$var = $field->name;
$$var = null;
$fields[$i] = &$$var;
$i++;
}
// Bind Results
call_user_func_array(array($stmt,'bind_result'),$fields);
$objects = Array();
// loop through results
while($stmt->fetch())
{
$object = new $cName;
$i = 0;
foreach($object->getFieldNames() as $fn)
{
$object->data[$fn] = $fields[$i];
$i++;
}
$objects[] = $object;
}
$stmt->close();
$conn->close();
return $objects;
}
public static function getAll()
{
return self::getAllWhere();
}
public static function getTableName()
{
$cName = get_called_class();
return camelToSnake($cName) .'s';
}
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function __get($name)
{
if (array_key_exists($name, $this->data))
{
return $this->data[$name];
}
else
{
return null;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment