Ferreteria/v2/class/ftMultiKeyedTable

From Woozle Writes Code
< Ferreteria‎ | v2‎ | class
Jump to navigation Jump to search

Template:Page/code/class

Functions

  • public function GetSeparatorCharacter()
    • RETURNS: character for separating multiple keys in a single string
    • PUBLIC so recordset can access it
  • abstract public function GetKeyNames();
    • NEW
    • PUBLIC so Records can access
    • RETURNS: array of key field names
  • public function GetRecord_forKey($id)
    • INPUT: $id = value which uniquely identifies the wanted row (primary keys combined in a separable way)
    • ASSUMES: For now --
      • values of keys in multi-key tables do not include $this->GetSeparatorCharacter()
      • in key strings, key values will always be separated by $this->GetSeparatorCharacter()

Code

/*
  PURPOSE: tables with multiple keys, and their recordsets
  HISTORY:
    2017-01-06 started
    2017-03-24 some rearrangements to make GetRecord_forKeyString() possible
    2017-04-04 moved multi-key stuff into ftMultiKeyedTable
*/
trait ftMultiKeyedTable {
    use ftKeyedTable;

    // ++ SETUP ++ //

    /*----
      RETURNS: character for separating multiple keys in a single string
      PUBLIC so recordset can access it
    */
    public function GetSeparatorCharacter() {
	return '.';
    }
    /*----
      NEW
      PUBLIC so Records can access
      RETURNS: array of key field names
    */
    abstract public function GetKeyNames();

    // -- SETUP -- //
    // ++ RECORDS ++ //

    /*----
      INPUT: $id = value which uniquely identifies the wanted row (primary keys combined in a separable way)
      ASSUMES: For now --
	* values of keys in multi-key tables do not include $this->GetSeparatorCharacter()
	* in key strings, key values will always be separated by $this->GetSeparatorCharacter()
    */
    public function GetRecord_forKey($id) {
	$arVals = explode($this->GetSeparatorCharacter(),$id);
	$arNames = $this->GetKeyNames();
	$sqlFilt = NULL;
	$db = $this->GetConnection();
	foreach ($arVals as $nVal) {
	    if (!is_null($sqlFilt)) {
		$sqlFilt .= ' AND ';
	    }
	    $sName = array_shift($arNames);
	    $sqlVal = $db->Sanitize_andQuote($nVal);
	    $sqlFilt .= "(`$sName`=$sqlVal)";
	}
	$rc = $this->SelectRecords($sqlFilt);
	if ($rc->RowCount() == 0) {
	    $rc->ClearFields();	// so HasRow() will return FALSE
	} else {
	    $rc->NextRow();	// advance to first (only) row
	}
	return $rc;
    }

    // -- RECORDS -- //

}