CakePHP $ acts除了在AppModel中没有效果?

时间:2012-11-27 15:13:02

标签: php cakephp

我正在尝试在CakePHP中实现一个类别树。

如果我在AppModel中添加此行

    public $actsAs = array('Tree');

并从 CategoriesController:

输出
    $behaviors = $this->Category->Behaviors->attached();
    debug($behaviors); die;

结果是:

    array(
            (int) 0 => 'Tree'
    )

但我不希望我的所有模特都表现得像树。因此,如果我在类别模型(Category.php)中添加它:

    public $actsAs = array('Tree');

CategoriesController:

输出
    $behaviors = $this->Category->Behaviors->attached();
    debug($behaviors); die;

结果是:

    array()

知道为什么吗? 这里的食谱:http://book.cakephp.org/2.0/en/models/behaviors.html#using-behaviors说我可以在我需要特定行为的模型中使用$ actsAs。

谢谢。

1 个答案:

答案 0 :(得分:1)

在这个例子中,我正在从控制器动态改变路径。

代码:

在Controller中(您指定数据以更改$ actsAs变量的值):

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

在模型中,您将在构造函数中更改值:

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);
}
  • PS:请在创建模型之前写下语句Configure :: write :-P
  • HAPPY CODING