Exemplo n.º 1
0
 /**
  * @param CActiveRecord $object
  * @param string $attribute
  */
 protected function validateAttribute($object, $attribute)
 {
     /**
      * Значение аттрибута
      */
     $value = $object->{$attribute};
     foreach ($object->getRelated('questions') as $question) {
         /** @var $question QuizQuestion */
         $pk = $question->getPrimaryKey();
         if (!is_array($value) || !array_key_exists($pk, $value) || $this->isEmpty($value[$pk], true)) {
             // если не пришли данные для этого вопроса, или пришли, но пустые
             $this->addError($object, $attribute, $pk);
         } else {
             // собираем PK ответов на этот вопрос
             $answers_pk = array();
             foreach ($question->getRelated('answers') as $answer) {
                 /** @var $answer QuizAnswer */
                 $answers_pk[] = $answer->getPrimaryKey();
             }
             if ($question->type == QuizQuestion::TYPE_SINGLE && !in_array($value[$pk], $answers_pk)) {
                 unset($object->{$attribute}[$pk]);
                 // стираем текущие значения
                 $this->addError($object, $attribute, $pk);
             }
             if ($question->type == QuizQuestion::TYPE_MULTIPLE) {
                 // на вопрос можно несколько ответов
                 /**
                  * "Лишние" ответы, не присутсвующие в списке вопросов
                  */
                 $wrong_answers = array_diff($value[$pk], $answers_pk);
                 if (!empty($wrong_answers)) {
                     unset($object->{$attribute}[$pk]);
                     // стираем текущие значения
                     $this->addError($object, $attribute, $pk);
                 }
             }
             /**
              * Дополнительной проверки произвольного ответа не производится
              * он не пуст и нас это устраивает
              */
         }
     }
 }
 /**
  *
  * @param CActiveRecord $model
  * @param mixed $attributes If is set to null all attributes of the model will be added. If a string is given, only this attribute will added. If an array of strings is given, all elements will be retrieved.
  * @param mixed $relations If is set to true, all relations will be retrieved. If a string is given, only one relation will be added. For array see the class definition of relations.
  */
 protected function convertModel($model, $attributes = null, $relations = null)
 {
     $relationArray = array();
     if ($attributes === null) {
         $attributes = array();
         foreach ($model->attributes as $attr => $type) {
             $attributes[] = $attr;
         }
     }
     if ($relations === true || is_array($relations) || is_string($relations)) {
         if ($relations === true) {
             //include all relations
             $relations = array();
             foreach ($model->getMetaData()->relations as $name => $relation) {
                 $relations[] = $name;
             }
         }
         if (is_string($relations)) {
             $relations = array($relations);
         }
         foreach ($relations as $key => $relation) {
             $relAttributes = null;
             $relRelations = null;
             /**
              * @var array|mixed array or CDbCriteria object with additional parameters that customize the query
              * conditions as specified in the relation declaration. See more at http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getRelated-detail
              */
             $relParams = array();
             /**
              * @var boolean whether to reload the related objects from database. See more at
              * http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getRelated-detail
              */
             $relRefresh = false;
             $relationName = $relation;
             if (is_array($relation)) {
                 $relationName = $key;
                 if (!isset($relation['attributes']) && !isset($relation['relations']) && !isset($relation['refresh']) && !isset($relation['params'])) {
                     // for convenient configuration
                     $relAttributes = $relation;
                 } else {
                     $relAttributes = isset($relation['attributes']) ? $relation['attributes'] : null;
                     $relRelations = isset($relation['relations']) ? $relation['relations'] : null;
                     $relParams = isset($relation['params']) ? $relation['params'] : array();
                     $relRefresh = isset($relation['refresh']) ? $relation['refresh'] : false;
                 }
             }
             $relatedModels = $model->getRelated($relationName, $relRefresh, $relParams);
             if (is_array($relatedModels)) {
                 foreach ($relatedModels as $relatedModel) {
                     $relationArray[$relationName][] = $this->convertModel($relatedModel, $relAttributes, $relRelations);
                 }
             } else {
                 if ($relatedModels) {
                     $relationArray[$relationName] = $this->convertModel($relatedModels, $relAttributes, $relRelations);
                 }
             }
         }
     }
     if (is_string($attributes)) {
         $attributes = array($attributes);
     }
     if ($attributes === null) {
         $attributes = true;
     }
     foreach ($attributes as $attribute) {
         $attributeArray[$attribute] = $model->{$attribute};
     }
     if ($this->attributeAliases) {
         $tempArray = array();
         foreach ($attributeArray as $attributeName => $value) {
             if (isset($this->attributeAliases[$attributeName])) {
                 $tempArray[$this->attributeAliases[$attributeName]] = $value;
             } else {
                 $tempArray[$attributeName] = $value;
             }
         }
         $attributeArray = $tempArray;
     }
     if ($this->onAfterModelToArray) {
         call_user_func_array($this->onAfterModelToArray, array($model, &$attributeArray, &$relationArray));
     }
     return array_merge($attributeArray, $relationArray);
 }
 /**
  *
  * @param CActiveRecord $model
  * @param mixed $attributes If is set to null all attributes of the model will be added. If a string is given, only this attribute will added. If an array of strings is given, all elements will be retrieved.
  * @param mixed $relations If is set to true, all relations will be retrieved. If a string is given, only one relation will be added. For array see the class definition of relations.
  */
 protected function convertModel($model, $attributes = null, $relations = null)
 {
     $relationArray = array();
     if ($attributes === null) {
         $attributes = array();
         foreach ($model->attributes as $attr => $type) {
             $attributes[] = $attr;
         }
     }
     if ($relations === true || is_array($relations) || is_string($relations)) {
         if ($relations === true) {
             //include all relations
             $relations = array();
             foreach ($model->getMetaData()->relations as $name => $relation) {
                 $relations[] = $name;
             }
         }
         if (is_string($relations)) {
             $relations = array($relations);
         }
         foreach ($relations as $key => $relation) {
             $relAttributes = null;
             $relRelations = null;
             $relationName = $relation;
             if (is_array($relation)) {
                 $relationName = $key;
                 if (!isset($relation['attributes']) && !isset($relation['relations'])) {
                     // for convenient configuration
                     $relAttributes = $relation;
                 } else {
                     $relAttributes = isset($relation['attributes']) ? $relation['attributes'] : null;
                     $relRelations = isset($relation['relations']) ? $relation['relations'] : null;
                 }
             }
             $relatedModels = $model->getRelated($relationName);
             if (is_array($relatedModels)) {
                 foreach ($relatedModels as $relatedModel) {
                     $relationArray[$relationName][] = $this->convertModel($relatedModel, $relAttributes, $relRelations);
                 }
             } else {
                 if ($relatedModels) {
                     $relationArray[$relationName] = $this->convertModel($relatedModels, $relAttributes, $relRelations);
                 }
             }
         }
     }
     if (is_string($attributes)) {
         $attributes = array($attributes);
     }
     if ($attributes === null) {
         $attributes = true;
     }
     foreach ($attributes as $attribute) {
         $attributeArray[$attribute] = $model->{$attribute};
     }
     if ($this->attributeAliases) {
         $tempArray = array();
         foreach ($attributeArray as $attributeName => $value) {
             if (isset($this->attributeAliases[$attributeName])) {
                 $tempArray[$this->attributeAliases[$attributeName]] = $value;
             } else {
                 $tempArray[$attributeName] = $value;
             }
         }
         $attributeArray = $tempArray;
     }
     if ($this->onAfterModelToArray) {
         call_user_func_array($this->onAfterModelToArray, array($model, &$attributeArray, &$relationArray));
     }
     return array_merge($attributeArray, $relationArray);
 }