FOF30\Model\DataModel::getTableFields PHP Method

getTableFields() public method

Get the columns from a database table.
public getTableFields ( string $tableName = null ) : mixed
$tableName string Table name. If null current table is used
return mixed An array of the field names, or false if an error occurs.
    public function getTableFields($tableName = null)
    {
        // Make sure we have a list of tables in this db
        if (empty(static::$tableCache)) {
            static::$tableCache = $this->getDbo()->getTableList();
        }
        if (!$tableName) {
            $tableName = $this->tableName;
        }
        // Try to load again column specifications if the table is not loaded OR if it's loaded and
        // the previous call returned an error
        if (!array_key_exists($tableName, static::$tableFieldCache) || isset(static::$tableFieldCache[$tableName]) && !static::$tableFieldCache[$tableName]) {
            // Lookup the fields for this table only once.
            $name = $tableName;
            $prefix = $this->getDbo()->getPrefix();
            if (substr($name, 0, 3) == '#__') {
                $checkName = $prefix . substr($name, 3);
            } else {
                $checkName = $name;
            }
            // Iterate through all lower/uppercase permutations of the prefix if we have a prefix with at least one uppercase letter
            if (!in_array($checkName, static::$tableCache) && preg_match('/[A-Z]/', $prefix) && substr($name, 0, 3) == '#__') {
                $prefixPermutations = $this->getPrefixCasePermutations();
                $partialCheckName = substr($name, 3);
                foreach ($prefixPermutations as $permutatedPrefix) {
                    $checkName = $permutatedPrefix . $partialCheckName;
                    if (in_array($checkName, static::$tableCache)) {
                        break;
                    }
                }
            }
            if (!in_array($checkName, static::$tableCache)) {
                // The table doesn't exist. Return false.
                static::$tableFieldCache[$tableName] = false;
            } else {
                $fields = $this->getDbo()->getTableColumns($name, false);
                if (empty($fields)) {
                    $fields = false;
                }
                static::$tableFieldCache[$tableName] = $fields;
            }
            // PostgreSQL date type compatibility
            if ($this->getDbo()->name == 'postgresql' && static::$tableFieldCache[$tableName] != false) {
                foreach (static::$tableFieldCache[$tableName] as $field) {
                    if (strtolower($field->type) == 'timestamp without time zone') {
                        if (stristr($field->Default, '\'::timestamp without time zone')) {
                            list($date, ) = explode('::', $field->Default, 2);
                            $field->Default = trim($date, "'");
                        }
                    }
                }
            }
        }
        return static::$tableFieldCache[$tableName];
    }

Usage Example

コード例 #1
0
ファイル: GenericList.php プロジェクト: Joal01/fof
 /**
  * Replace string with tags that reference fields
  *
  * @param   string  $text  Text to process
  *
  * @return  string         Text with tags replace
  */
 protected function parseFieldTags($text)
 {
     $ret = $text;
     // Replace [ITEM:ID] in the URL with the item's key value (usually:
     // the auto-incrementing numeric ID)
     $keyfield = $this->item->getKeyName();
     $replace = $this->item->{$keyfield};
     $ret = str_replace('[ITEM:ID]', $replace, $ret);
     // Replace the [ITEMID] in the URL with the current Itemid parameter
     $ret = str_replace('[ITEMID]', $this->form->getContainer()->input->getInt('Itemid', 0), $ret);
     // Replace other field variables in the URL
     $fields = $this->item->getTableFields();
     foreach ($fields as $fielddata) {
         $fieldname = $fielddata->Field;
         if (empty($fieldname)) {
             $fieldname = $fielddata->column_name;
         }
         $search = '[ITEM:' . strtoupper($fieldname) . ']';
         $replace = $this->item->{$fieldname};
         if (!is_string($replace)) {
             continue;
         }
         $ret = str_replace($search, $replace, $ret);
     }
     return $ret;
 }
All Usage Examples Of FOF30\Model\DataModel::getTableFields