DB_dsql::field PHP Méthode

field() public méthode

Examples: $q->field('name'); Second argument specifies table for regular fields $q->field('name','user'); $q->field('name','user')->field('line1','address'); Array as a first argument will specify mulitple fields, same as calling field() multiple times $q->field(array('name','surname')); Associative array will assume that "key" holds the alias. Value may be object. $q->field(array('alias'=>'name','alias2'=>surname')); $q->field(array('alias'=>$q->expr(..), 'alias2'=>$q->dsql()->.. )); You may use array with aliases together with table specifier. $q->field(array('alias'=>'name','alias2'=>surname'),'user'); You can specify $q->expr() for calculated fields. Alias is mandatory. $q->field( $q->expr('2+2'),'alias'); // must always use alias You can use $q->dsql() for subqueries. Alias is mandatory. $q->field( $q->dsql()->table('x')... , 'alias'); // must always use alias
public field ( string | array $field, string $table = null, string $alias = null )
$field string | array Specifies field to select
$table string Specify if not using primary table
$alias string Specify alias for this field
    public function field($field, $table = null, $alias = null)
    {
        if (is_string($field) && strpos($field, ',') !== false) {
            $field = explode(',', $field);
        } elseif (is_object($field)) {
            $alias = $table;
            $table = null;
        }
        if (is_array($field)) {
            foreach ($field as $alias => $f) {
                if (is_numeric($alias)) {
                    $alias = null;
                }
                $this->field($f, $table, $alias);
            }
            return $this;
        }
        $this->args['fields'][] = array($field, $table, $alias);
        return $this;
    }

Usage Example

Exemple #1
0
 /**
  * Modifies specified query to include this particular field.
  *
  * @param DB_dsql $select
  *
  * @return $this
  */
 public function updateSelectQuery($select)
 {
     $p = null;
     if (!empty($this->owner->relations)) {
         $p = $this->owner->table_alias ?: $this->owner->table;
     }
     if ($this->relation) {
         $select->field($this->actual_field ?: $this->short_name, $this->relation->short_name, $this->short_name);
     } elseif (!is_null($this->actual_field) && $this->actual_field != $this->short_name) {
         $select->field($this->actual_field, $p, $this->short_name);
         return $this;
     } else {
         $select->field($this->short_name, $p);
     }
     return $this;
 }