Change $ actAs variable from controller

All I want to do is change the value ("path") in the $ actAs array of my controller boot model before saving.

The only way I came in was to unload the behavior, and then load it with all its settings.

It seems unnecessary to just change the path. Is there an easier / better way?

$this->loadModel('Upload');
$this->Upload->Behaviors->unload('Upload.Upload');
$this->Upload->Behaviors->load('Upload.Upload', array(
    'photo' => array(
        'thumbnailSizes' => array(
            'xvga' => '1024x768',
            'vga' => '640x480',
            'thumb' => '80x80',
        ),
        'thumbnailMethod' => 'php',
        'thumbnailQuality' => '80',
        'pathMethod'=>'random',
        'path' => '{ROOT}webroot{DS}uploads{DS}test{DS}{field}{DS}',
        'maxSize' => '5242880', //5MB
        'mimetypes' => array('image/jpeg', 'image/png', 'image/gif', 'image/bmp'),
        'extensions' => array('jpg', 'gif', 'png', 'bmp'),
    ),
));
if($this->Upload->save($this->request->data)) {
//...
+3
source share
2 answers

From looking at the source, I think the method uploadSettings()is what you are looking for:

$this->Upload->uploadSettings('photo', 'path', 'YOUR_PATH');
0
source

The BehaviorCollection must merge the configuration when it is loaded according to the existing behavior, so you do not need to re-enable all other configurations. In fact, you also do not need to unload it.

$this->Upload->Behaviors->load('Upload.Upload', array(
    'photo' => array(
        'path' => '{ROOT}webroot{DS}uploads{DS}test{DS}{field}{DS}'
    )
));

array_merge array_merge_recursive, . , , , , , , , Cake , .

$actsAs , .

public function __construct($id = false, $table = null, $ds = null) {
  // 2 lines for readability
  $this->actsAs['Upload.Upload']['photo']['path'] = 
  '{ROOT}webroot{DS}uploads{DS}test{DS}{field}{DS}';
  return parent::__construct($id, $table, $ds);
}
0

All Articles