Risto/Model/Behavior/MediaUploadableBehavior.php
function beforeSave( Model $Model, $options = array() ){
	  	if ( !array_key_exists('Media', $Model->belongsTo) ) {
		  	$Model->bindModel(
		        array('belongsTo' => array(
		                'Media' => array(
		                    'className' => 'Risto.Media'
		                )
		            )
		        )
		    );
	  	}

	    if( isset($Model->data[$Model->alias][$this->_form_field_name]) 
	    	&& isset( $Model->data[$Model->alias][$this->_form_field_name]['error'] )
	    	&& $Model->data[$Model->alias][$this->_form_field_name]['error'] == 0
	    	){
	      $data = array('Media' => $Model->data[$Model->alias][$this->_form_field_name]);
	      $data['Media']['file'] = file_get_contents($Model->data[$Model->alias][$this->_form_field_name]['tmp_name']);
	      $data['Media']['model'] = $Model->name;
	      $Model->Media->create();
	      if ( !$Model->Media->save($data)) {
	      		// error
	      		$Model->validationErrors[$this->_form_field_name] = $Model->Media->validationErrors;
	      		return false;
	      }
      	  $Model->data[$Model->alias][$this->_form_fk] = $Model->Media->id;
      	  
	    }

	    return true;
	  }
function beforeSave( Model $Model, $options = array() ){
	  	if ( !array_key_exists('Media', $Model->belongsTo) ) {
		  	$Model->bindModel(
		        array('belongsTo' => array(
		                'Media' => array(
		                    'className' => 'Risto.Media'
		                )
		            )
		        )
		    );
	  	}

	    if( isset($Model->data[$Model->alias][$this->_form_field_name]) 
	    	&& isset( $Model->data[$Model->alias][$this->_form_field_name]['error'] )
	    	&& $Model->data[$Model->alias][$this->_form_field_name]['error'] == 0
	    	){
	      $data = array('Media' => $Model->data[$Model->alias][$this->_form_field_name]);
	      $data['Media']['file'] = file_get_contents($Model->data[$Model->alias][$this->_form_field_name]['tmp_name']);
	      $data['Media']['model'] = $Model->name;
	      $Model->Media->create();
	      if ( !$Model->Media->save($data)) {
	      		// error
	      		$Model->validationErrors[$this->_form_field_name] = $Model->Media->validationErrors;
	      		return false;
	      }
      	  $Model->data[$Model->alias][$this->_form_fk] = $Model->Media->id;
      	  
	    }

	    return true;
	  }
function beforeSave ( Model $ Model , $ options = array ( ) ) { if ( ! array_key_exists ( 'Media' , $ Model -> belongsTo ) ) { $ Model -> bindModel ( array ( 'belongsTo' => array ( 'Media' => array ( 'className' => 'Risto.Media' ) ) ) ) ; } if ( isset ( $ Model -> data [ $ Model -> alias ] [ $ this -> _form_field_name ] ) && isset ( $ Model -> data [ $ Model -> alias ] [ $ this -> _form_field_name ] [ 'error' ] ) && $ Model -> data [ $ Model -> alias ] [ $ this -> _form_field_name ] [ 'error' ] == 0 ) { $ data = array ( 'Media' => $ Model -> data [ $ Model -> alias ] [ $ this -> _form_field_name ] ) ; $ data [ 'Media' ] [ 'file' ] = file_get_contents ( $ Model -> data [ $ Model -> alias ] [ $ this -> _form_field_name ] [ 'tmp_name' ] ) ; $ data [ 'Media' ] [ 'model' ] = $ Model -> name ; $ Model -> Media -> create ( ) ; if ( ! $ Model -> Media -> save ( $ data ) ) { // error $ Model -> validationErrors [ $ this -> _form_field_name ] = $ Model -> Media -> validationErrors ; return false ; } $ Model -> data [ $ Model -> alias ] [ $ this -> _form_fk ] = $ Model -> Media -> id ; } return true ; }
beforeSave if a file is found, upload it, and then save the filename according to the settings
beforeSave if a file is found upload it and then save the filename according to the settings