Пример #1
0
 /**
  * Get available relation choices
  * @param $relation_name
  * @return mixed
  */
 public static function getSelectChoices(ActiveRecord $model, $relation_name)
 {
     $class = $model->className();
     if (!isset(self::$relationsChoices[$class][$relation_name])) {
         self::$relationsChoices[$class][$relation_name] = [];
         $relation = $model->getRelation($relation_name, false);
         if ($relation) {
             self::$relationsChoices[$class][$relation_name] = ModelHelper::getSelectChoices(new $relation->modelClass());
         }
     }
     return self::$relationsChoices[$class][$relation_name];
 }
Пример #2
0
 protected static function editableRelationConfig(ActiveRecord $model, $relation_name)
 {
     /* @var ActiveRecord|YcmModelUtilTrait $model */
     $relation = $model->getRelation($relation_name);
     /* @var Module $module */
     $module = \Yii::$app->getModule('ycm-utils');
     /* @var ActiveRecord|YcmModelUtilTrait $relationModel */
     $relationModel = \Yii::createObject($relation->modelClass);
     $modelChoices = ModelHelper::getSelectChoices($relationModel);
     /**
      * @todo #1 implement fill ajax loading with ajax mapping
      * @fixme #2 Relation is multiple, after live edit fix JS error `Cannot read property '[object Array]' of null`
      */
     return ['attribute' => $relation->multiple ? $relation_name . $model->method_postfix_relation_ids : reset($relation->link), 'label' => ucfirst($relation_name), 'filter' => $modelChoices, 'value' => $relation->multiple ? function ($m) use($relation_name, $modelChoices, $model) {
         return implode(', ', array_map(function ($relation_id) use($modelChoices) {
             return $modelChoices[$relation_id];
         }, array_values((array) $m->{$relation_name . $model->method_postfix_relation_ids})));
     } : null, 'editableOptions' => ['inputType' => Editable::INPUT_SELECT2, 'size' => 'lg', 'options' => ['options' => ['multiple' => $relation->multiple], 'data' => $modelChoices, 'pluginOptions' => count($modelChoices) > $model->ajax_enable_threshold ? ['minimumInputLength' => 3, 'ajax' => ['url' => Url::to(['/ycm/model/choices', 'name' => $module->ycm->getModelName($relationModel)]), 'dataType' => 'json', 'processResults' => new JsExpression('function (results) { return results; }')]] : null], 'displayValueConfig' => !$relation->multiple ? $modelChoices : null]];
 }
Пример #3
0
 public static function enumerate(ActiveRecord $model, $attribute, $options = [])
 {
     $choices = ModelHelper::getEnumChoices($model, $attribute);
     $config = [$attribute, 'widget', 'widgetClass' => Select2::className(), 'data' => $choices, 'options' => ['placeholder' => 'Select...']];
     return ArrayHelper::merge($config, $options);
 }
 function __toString()
 {
     /* @var ActiveRecord|static $this */
     return ModelHelper::getLabelColumnValue($this);
 }