public function loadDataFromTableOrView($resultItemTypeName, $tableName, SqlWhere $where = NULL) { $sql = "SELECT "; $properties = get_class_vars($resultItemTypeName); $propertyIndex = 0; foreach ($properties as $propertyName => $propertyValue) { $sql .= $propertyIndex == 0 ? $propertyName : ", " . $propertyName; $propertyIndex++; } $sql .= " FROM " . $tableName; if ($where == NULL) { $sql .= ";"; } else { $sql .= " WHERE " . $where->getSql() . ";"; } $statement = $this->db->prepare($sql); if ($where != NULL) { $values = $where->getBindParamArray(); $arr = array(); for ($i = 0; $i < count($values); $i++) { if ($i == 0) { $arr[$i] = $values[$i]; } else { $arr[$i] =& $values[$i]; } } call_user_func_array(array($statement, "bind_param"), $arr); } $statement->execute(); return $this->bindResultsAndCreateResultItems($statement, $resultItemTypeName); }
echo "<br /><br />"; // Load data from a stored procedure (without parameters) $people = $dbHelper->loadDataFromStoredProcedure(Person . __CLASS__, "getPeople"); echo "getPeople<br />"; echo json_encode($people); echo "<br /><br />"; // Load data from a table $people = $dbHelper->loadDataFromTableOrView(Person . __CLASS__, "Person"); echo "Person table data<br />"; echo json_encode($people); echo "<br /><br />"; // Load data from a view $people = $dbHelper->loadDataFromTableOrView(Person . __CLASS__, "AllPeople"); echo "AllPeople view data<br />"; echo json_encode($people); echo "<br /><br />"; $where = new SqlWhere(); $where->addCondition(ConditionType::NONE, "first_name", "=", "Piero", ParameterType::STRING)->addCondition(ConditionType::_OR, "first_name", "=", "Marco", ParameterType::STRING); // Load data from a view $people = $dbHelper->loadDataFromTableOrView(Person . __CLASS__, "AllPeople", $where); echo "AllPeople view data (filtered)<br />"; echo json_encode($people); echo "<br /><br />"; $where = new SqlWhere(); $where->addCondition(ConditionType::NONE, "first_name", "LIKE", "%er%", ParameterType::STRING); // Load data from a view $people = $dbHelper->loadDataFromTableOrView(Person . __CLASS__, "AllPeople", $where); echo "AllPeople view data (filtered with LIKE condition)<br />"; echo json_encode($people); echo "<br /><br />"; $dbHelper->close();