[ Pobierz całość w formacie PDF ]
.The only exception is the protected internal methodsetBareQuery, which skips the translation.Translation code is not shown here.It is simple in concept, but the fact that #__ mayoccur within quoted strings complicates matters.Within strings, #__ should not beshould not betranslated.The text in quotes may, for example, be a message in a forum discussing.The text in quotes may, for example, be a message in a forum discussingcode, and it is thus completely wrong for any translation to take place.This issuemakes the translation code quite tedious.Like all of Aliro, it is readily availablefor download.Making the Database WorkThe simplest method provided for invoking SQL operations is setQuery.It doesnothing more than translate any table name prefix codes, and then store the SQL:// Replaces #__ by the chosen database prefix and saves the querypublic function setQuery( $sql, $cached=false, $prefix='#__' ){$this->_sql = $this->replacePrefix($sql, $prefix);$this->_cached = $cached;}[ 101 ]Database and Data ObjectsNote that the last parameter allows for the possibility of using an alternative codeinstead of #__ to indicate the prefix when writing SQL.This might well be anunnecessary complication, but has been retained for compatibility with Aliro'spredecessor systems, discussed earlier in Chapter 1.Although there is provision forspecifying that the result of the query should be cached, at the time of writing, thisparticular form of cache is not implemented in Aliro.Reasons for this are discussedin Chapter 8 on caches.The important feature of this method is that it invokes a translation of a code into theprefix chosen for this installation, and then stores the translated SQL.For debugging, it is very useful to be able to get the SQL back again in its translatedform.The result can be displayed, and a developer can feed it directly into thedatabase to check out what happens.// Returns stored SQL with replacements, ready to displaypublic function getQuery ($sql=''){if ($sql == '') $sql = $this->_sql;return "".htmlspecialchars( $sql )."";}Having stored an SQL statement, the method query makes a call to the database toexecute the SQL.The result is a cursor that is linked to the result set, but the cursor iskept internal to the database class and the various result retrieval methods are usedto obtain information about the effects of the SQL.The query method's code is not ofparticular interest.Later, methods are described that avoid the need to use the basicsetQuery, and query methods.Getting Hold of DataThe simplest way to handle the results from a query is to put the whole result setinto a suitable PHP variable for processing, especially as memory is now availablein generous quantities.This assumes that care is being taken to retrieve only thedata needed for a particular display, such as a single page of items.Different kindsof queries can be handled in different ways.To make implementation efficient, thevarious data access methods all rely on a single internal general purpose method:protected function retrieveResults ($key='', $max=0,$result_type='row'){$results = array();if (!in_array($result_type, array ('row', 'object', 'assoc')))die ('unexpected result type='.$result_type);$sql_function = 'mysqli_fetch_'.$result_type;[ 102 ]Chapter 5if ($cur = $this->query()){while ($row = $sql_function($cur)){if ($key != '') $results[(is_array($row) ? $row[$key] :$row->$key)] = $row;else $results[] = $row;if ($max AND count($results) >= $max) break;}mysqli_free_result($cur);}return $results;}If the name of a key is specified, then the results are provided as an array with thekey values as the array subscripts.Otherwise, the results are a simple array withnumeric subscripts.The maximum number of results can be specified, and thereare three options for the kind of result set: row, object, or assoc corresponding tothe available PHP interface functions.The base method is only accessible within thedatabase class, but is used by a series of public methods.The simplest is loadResult:public function loadResult(){$results = $this->retrieveResults('', 1, 'row');if (count($results)) return $results[0][0];else return null;}It uses the protected method retrieveResults to obtain a single database row asan array, and then returns the first value of that first row.Typically, it is used withSQL that is expected to yield only a single value.When the SQL is expected toyield a number of rows, but only a single value is wanted, the loadResultArraymethod is suitable:public function loadResultArray($numinarray = 0){$results = $this->retrieveResults('', 0, 'row');foreach ($results as $result) $values[] = $result[$numinarray];return isset($values) ? $values : null;}[ 103 ]Database and Data ObjectsThe return of null when there are no results is often an unhelpful choice.A zero sizedarray would usually be much more convenient
[ Pobierz całość w formacie PDF ]