fields() public method

A field is a named element in the returned array by [[toArray()]]. This method should return an array of field names or field definitions. If the former, the field name will be treated as an object property name whose value will be used as the field value. If the latter, the array key should be the field name while the array value should be the corresponding field definition which can be either an object property name or a PHP callable returning the corresponding field value. The signature of the callable should be: php function ($model, $field) { return field value } For example, the following code declares four fields: - email: the field name is the same as the property name email; - firstName and lastName: the field names are firstName and lastName, and their values are obtained from the first_name and last_name properties; - fullName: the field name is fullName. Its value is obtained by concatenating first_name and last_name. php return [ 'email', 'firstName' => 'first_name', 'lastName' => 'last_name', 'fullName' => function ($model) { return $model->first_name . ' ' . $model->last_name; }, ]; In this method, you may also want to return different lists of fields based on some context information. For example, depending on [[scenario]] or the privilege of the current application user, you may return different sets of visible fields or filter out some fields. The default implementation of this method returns Model::attributes indexed by the same attribute names.
See also: toArray()
public fields ( ) : array
return array the list of field names or field definitions.
Example #1
0
 private function doMasking($fields){
     $fieldMasks = $this->fieldMasks();
     $allFields = parent::fields();
     if(!is_array($fields) || count($fields) == 0){
         $fields = $allFields;
     }
     if(!is_array($fieldMasks) || count($fieldMasks) == 0){
         return $fields;
     }
     $fieldMasks = array_merge($allFields,$fieldMasks);
     $_fields = [];
     foreach($fields as $key=>$value){
         $_key = $key;
         if(!is_string($key) && is_string($value)){
             $_key = $value;
         }
         if(isset($fieldMasks[$_key])){
             $_key = $fieldMasks[$_key];
         }
         $_fields[$_key] = $value;
     }
     return $_fields;
 }
Example #2
0
 public function fields()
 {
     $fields = parent::fields();
     unset($fields['pass'], $fields['confirm']);
     return $fields;
 }
Example #3
0
 public function fields()
 {
     $fields = parent::fields();
     $fields['id'] = 'id';
     return $fields;
 }
Example #4
0
 public function fields()
 {
     return ArrayHelper::merge(parent::fields(), ['coords']);
 }