getAttributes() public method

Returns attribute values.
public getAttributes ( array $names = null, array $except = [] ) : array
$names array list of attributes whose value needs to be returned. Defaults to null, meaning all attributes listed in [[attributes()]] will be returned. If it is an array, only the attributes in the array will be returned.
$except array list of attributes whose value should NOT be returned.
return array attribute values (name => value).
Example #1
1
 /**
  * @param $values
  * @param Model $model
  */
 protected function getAttributes(&$values, Model $model)
 {
     foreach ($model->getAttributes() as $attribute => $value) {
         $className = explode('\\', get_class($model));
         $className = $className[count($className) - 1];
         $values[$this->getKeyFromAttribute($className, $attribute)] = $value;
     }
 }
Example #2
0
 /**
  * Retrieves grid columns configuration using the modelClass.
  * @param Model $model
  * @param array $fields
  * @return array grid columns
  */
 public function getIndexGridColumns($model, $fields)
 {
     $id = Yii::$app->request->getQueryParam('id');
     $relationName = Yii::$app->request->getQueryParam('relation');
     $multiple = Yii::$app->request->getQueryParam('multiple', 'true') === 'true';
     foreach ($fields as $key => $field) {
         if ((is_array($field) || !is_string($field) && is_callable($field)) && $key === $relationName || $field === $relationName) {
             unset($fields[$key]);
         }
     }
     return array_merge([['class' => 'yii\\grid\\CheckboxColumn', 'multiple' => $multiple, 'headerOptions' => ['class' => 'column-serial'], 'checkboxOptions' => function ($model, $key, $index, $column) use($id, $relationName) {
         /** @var \yii\db\ActiveRecord $model */
         $options = ['value' => is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key];
         if (empty($relationName) || $model->{$relationName} === null || trim($id) === '') {
             return $options;
         }
         $relation = $model->getRelation($relationName);
         if ($relation->multiple) {
             /** @var \yii\db\ActiveRecord $relationClass */
             $relationClass = $relation->modelClass;
             if (Action::importKey($relationClass::primaryKey(), $id) === $model->getAttributes(array_keys($relation->link))) {
                 $options['checked'] = true;
                 $options['disabled'] = true;
             }
         } elseif (Action::exportKey($relation->one()->getPrimaryKey()) === $id) {
             $options['checked'] = true;
             $options['disabled'] = true;
         }
         return $options;
     }], ['class' => 'yii\\grid\\SerialColumn', 'headerOptions' => ['class' => 'column-serial']]], self::getGridColumns($model, $fields));
 }
Example #3
0
 /**
  * @param $fields
  * @param $class
  * @param Model $model
  * @return array
  */
 protected function getFieldsByAttributes(&$fields, $class, Model $model)
 {
     $attributes = $model->getAttributes();
     if (!empty($attributes)) {
         foreach (array_keys($attributes) as $attribute) {
             $fields[] = $class . $this->classNameKeySeparator . $attribute;
         }
     }
 }
Example #4
0
 /**
  * Marshal a Yii2 model object to a new array that is formatted in
  * the proper parameter structure required by DynamoDB operations.
  *
  * @param \yii\base\Model $item The model to be marshalled.
  * @return mixed
  */
 public static function marshalModel(\yii\base\Model $item)
 {
     return self::marshaler()->marshalItem($item->getAttributes());
 }
Example #5
0
 /**
  * @return array
  */
 protected function getModelAttributes()
 {
     if ($this->searchModel === null || $this->modelClass === null) {
         return [];
     }
     $attributes = [];
     $model = new $this->modelClass();
     foreach ($this->searchModel->getAttributes() as $name => $value) {
         if ($value !== null) {
             $attributes[Html::getInputName($model, $name)] = $value;
         }
     }
     return $attributes;
 }
Example #6
0
 /**
  * Return errors as bulleted list for model
  *
  * @param Model $model
  *
  * @return string
  */
 public static function showErrors($model)
 {
     $errors = [];
     foreach ($model->getAttributes() as $attribute => $setting) {
         $error = $model->getFirstError($attribute);
         if (trim($error) != null) {
             $errors[] = $error;
         }
     }
     return '<ul><li>' . implode("</li>\n<li>", $errors) . '</li></ul>';
 }