getInstance() 공개 메소드

Generator method to return a new instance of the element type class.
public getInstance ( ) : BaseEventTypeElement
리턴 BaseEventTypeElement
 /**
  * Initialise an element of $element_type for returning as an individual form. If the $previous_id is provided,
  * then the default values of the element will be overridden with the properties of the previous intance of the
  * element. Similarly, $additional allows specific values to be set on the element.
  *
  * Abstracted to allow overrides in specific module controllers
  *
  * @param ElementType $element_type
  * @param integer $previous_id
  * @param array() $additional - additional attributes for the element
  * @return \BaseEventTypeElement
  */
 protected function getElementForElementForm($element_type, $previous_id = 0, $additional)
 {
     $element_class = $element_type->class_name;
     $element = $element_type->getInstance();
     $this->setElementDefaultOptions($element, "create");
     if ($previous_id && $element->canCopy()) {
         $previous_element = $element_class::model()->findByPk($previous_id);
         $element->loadFromExisting($previous_element);
     }
     if ($additional) {
         foreach (array_keys($additional) as $add) {
             if ($element->isAttributeSafe($add)) {
                 $element->{$add} = $additional[$add];
             }
         }
     }
     return $element;
 }
예제 #2
0
 /**
  * Processes provided form data to create 1 or more elements of the provided type.
  *
  * @param ElementType $element_type
  * @param $data
  * @return array
  * @throws Exception
  */
 protected function getElementsForElementType(ElementType $element_type, $data)
 {
     $elements = array();
     $el_cls_name = $element_type->class_name;
     $f_key = CHtml::modelName($el_cls_name);
     if (isset($data[$f_key])) {
         $keys = array_keys($data[$f_key]);
         if (is_array($data[$f_key][$keys[0]]) && !count(array_filter(array_keys($data[$f_key]), 'is_string'))) {
             // there is more than one element of this type
             $pk_field = $el_cls_name::model()->tableSchema->primaryKey;
             foreach ($data[$f_key] as $i => $attrs) {
                 if (!$this->event->isNewRecord && !isset($attrs[$pk_field])) {
                     throw new Exception('missing primary key field for multiple elements for editing an event');
                 }
                 if ($pk = @$attrs[$pk_field]) {
                     $element = $el_cls_name::model()->findByPk($pk);
                 } else {
                     $element = $element_type->getInstance();
                 }
                 $element->attributes = Helper::convertNHS2MySQL($attrs);
                 $this->setElementComplexAttributesFromData($element, $data, $i);
                 $element->event = $this->event;
                 $elements[] = $element;
             }
         } else {
             if ($this->event->isNewRecord || !($element = $el_cls_name::model()->find('event_id=?', array($this->event->id)))) {
                 $element = $element_type->getInstance();
             }
             $element->attributes = Helper::convertNHS2MySQL($data[$f_key]);
             $this->setElementComplexAttributesFromData($element, $data);
             $element->event = $this->event;
             $elements[] = $element;
         }
     }
     return $elements;
 }