當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Model::safeAttributes方法代碼示例

本文整理匯總了PHP中yii\base\Model::safeAttributes方法的典型用法代碼示例。如果您正苦於以下問題:PHP Model::safeAttributes方法的具體用法?PHP Model::safeAttributes怎麽用?PHP Model::safeAttributes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在yii\base\Model的用法示例。


在下文中一共展示了Model::safeAttributes方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: getFormFields

 /**
  * Retrieves form fields configuration. Fields can be config arrays, ActiveField objects or closures.
  *
  * @param \yii\base\Model|\netis\crud\db\ActiveRecord $model
  * @param array           $fields
  * @param bool            $multiple         true for multiple values inputs, usually used for search forms
  * @param array           $hiddenAttributes list of attribute names to render as hidden fields
  *
  * @return array form fields
  * @throws InvalidConfigException
  */
 public static function getFormFields($model, $fields, $multiple = false, $hiddenAttributes = [])
 {
     if (!$model instanceof \yii\db\ActiveRecord) {
         return $model->safeAttributes();
     }
     $keys = Action::getModelKeys($model);
     $hiddenAttributes = array_flip($hiddenAttributes);
     list($behaviorAttributes, $blameableAttributes) = Action::getModelBehaviorAttributes($model);
     $attributes = $model->safeAttributes();
     $relations = $model->relations();
     if (($versionAttribute = $model->optimisticLock()) !== null) {
         $hiddenAttributes[$versionAttribute] = true;
     }
     $formFields = [];
     foreach ($fields as $key => $field) {
         if ($field instanceof ActiveField) {
             $formFields[$key] = $field;
             continue;
         } elseif (!is_string($field) && is_callable($field)) {
             $formFields[$key] = call_user_func($field, $model);
             if (!is_string($formFields[$key])) {
                 throw new InvalidConfigException('Field definition must be either an ActiveField or a callable.');
             }
             continue;
         } elseif (!is_string($field)) {
             throw new InvalidConfigException('Field definition must be either an ActiveField or a callable.');
         }
         $attributeName = Html::getAttributeName($field);
         if (in_array($attributeName, $relations)) {
             $formFields = static::addRelationField($formFields, $model, $field, $hiddenAttributes, $attributes, $multiple);
         } elseif (in_array($attributeName, $attributes)) {
             if (in_array($attributeName, $keys) || in_array($attributeName, $behaviorAttributes)) {
                 continue;
             }
             $formFields = static::addFormField($formFields, $model, $field, $hiddenAttributes, $multiple);
         }
     }
     return $formFields;
 }
開發者ID:netis-pl,項目名稱:yii2-crud,代碼行數:50,代碼來源:FormBuilder.php


注:本文中的yii\base\Model::safeAttributes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。