开发者

using user defined variable in model class variable in cakephp

I want to use a form variable inside a cakephp model class variable $actsAs.Below is an example code.

public $actsAs = array('MeioUpload' => array('doc' => array('allowedMime' => array('application/x-compr开发者_运维百科essed','application/x-zip-compressed','application/zip','multipart/x-zip'),'dir'=>'uploads'.DS.$this->data['User']['foldername'])));

In the above code i have used a form variable ($this->data['User']['foldername']) in the $actsAs array for passing directory name to meioupload behaviour.

What can be the write process to implement it.


That definition is wrong.

You can set that data from the constructor though.

public function __construct($data) {
    $this->actsAs = array('MeioUpload' => array('doc' => array('allowedMime' => array('application/x-compressed', 'application/x-zip-compressed', 'application/zip', 'multipart/x-zip'), 'dir' => 'uploads' . DS . $data['User']['foldername'])));
}

Something like that would do the trick.


I added parent::__construct(); And $this->Behaviors->init($this->alias,$this->actsAs); and it works good.

Below is the modified code:

public function  __construct() {        
    parent::__construct();

    $this->actsAs = array('MeioUpload' => array('doc' => array('allowedMime' => array('application/x-compressed', 'application/x-zip-compressed', 'application/zip', 'multipart/x-zip'), 'allowedExt' => array('.zip'),'dir' => 'uploads' . DS . $_REQUEST['data']['User']['foldername'])));

    $this->Behaviors->init($this->alias,$this->actsAs);         
}


In this example I’m changing path dynamically from controller.

CODE :

In Controller ( Where you specifying data to change value of $actsAs variable ) :

$custom_path=’/img/cakephp’;
Configure::write(‘path_var’,$custom_path);

In Model where you’ll change value in constructor :

public function __construct($id = false, $table = null, $ds = null)
{
  $path = Configure::read(‘path_var’);
  // change actsAs’s different value according to your need
  $this->actsAs['Upload.Upload']['photo']['path'] = $path;
  parent::__construct($id, $table, $ds);
}

Please write statement Configure::write before model created.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜