controller = &$controller; } /** * Converts array from findAll* methods (nr.className.fieldName) in the format of className.recordId.fieldName * * @param array The data in the format nr.className.fieldName * @return array in the format className.recordId.fieldName */ function toMulti($data){ $primaryKey = $this->controller->{$this->controller->modelClass}->primaryKey; //usualy 'id' $result = array(); if (is_array($data)){ foreach ($data as $record) { if ($ar = @each($record)){ $result[$ar["value"][$primaryKey]][$ar["key"]] = $ar["value"]; } } } return $result; } /** * Validate mulitple records * On validate failure, the validation result will be passed to that model * * @return boolean True if all records validate */ function validate(){ $validationErrors = array(); $model = $this->controller->{$this->controller->modelClass}; foreach ($this->controller->data as $id=>$data) { $model->create($this->controller->data[$id]); //if doesn't validate, add to array if (!$model->validates()){ $validationErrors[$id][$this->controller->name] = $model->validationErrors; } } if (!empty($validationErrors)){ $model->validationErrors = $validationErrors; } return empty($validationErrors); } /** * Saves all records in this->controller->data * * @return boolean True if all records are saved */ function save(){ $model = $this->controller->{$this->controller->modelClass}; $result = $model->saveAll($this->controller->data, array("validate"=>false)); return $result; } } ?>