/** * Executes a NamedQuery with Params to be binded. * If TRUE is given as Param, the ResultSet of the Execution will be fetched. * Otherwise only the Success of the Execution is returned. * @param NamedQuery $namedQuery with its query string and the corresponding parameters * @param needsFetch boolean to indicate whether result has to be fetched or not */ private function namedQuery($namedQuery, $needsFetch, $className) { $types = ""; $values = array(); // append all types to a String and push values into an array foreach ($namedQuery->getParams() as $queryParam) { $types .= $queryParam->getType(); array_push($values, $queryParam->getValue()); } $params = array(); $params[] =& $types; // add Types to params array (by reference) for ($i = 0; $i < count($values); $i++) { $params[] =& $values[$i]; // add Values to params array (by reference) } // create a prepared statement $statement = self::$dbConn->prepare($namedQuery->getNamedQuery()); // Call the function with parameter array. We have to do this because // "bind_param" cannot be called particularly, instead has to be called like // bind_param("ssi", "hello", "world", 42) if (!empty($types)) { call_user_func_array(array($statement, 'bind_param'), $params); } // return boolen result of execution if (!$needsFetch) { $statement->execute(); return $statement->insert_id; } // execute the statement. $statement->execute(); $resultTable = array(); $result = $statement->get_result(); // loop over the fetched result set while ($row = $result->fetch_object()) { $resultTable[] = $this->fetchResult($row, $className); } // close the db-connection of result and statement $result->close(); $statement->close(); return $resultTable; }