如何在虚拟字段定义中使用配置值?

时间:2015-08-24 10:03:17

标签: php cakephp

我需要在虚拟字段中使用配置值,类似于:

public $virtualFields = array(
        'allow_modification' => ' CASE WHEN publish_date IS NULL OR publish_date + interval \''.Configure::read('Setting.app.allow_modification').'\' > NOW() THEN TRUE ELSE FALSE END'
    );

但它不起作用。我知道它与PHP有关,但是在CakePHP中如何解决和修改virtualFields呢?

1 个答案:

答案 0 :(得分:1)

virtualFields构造函数中设置Model(请参阅here):

class MyModel extends AppModel {
    public function __construct($id = false, $table = null, $ds = null) {
        parent::__construct($id, $table, $ds);
        $this->virtualFields['allow_modification'] = 'CASE WHEN publish_date IS NULL OR publish_date + interval \''.Configure::read('Setting.app.allow_modification').'\' > NOW() THEN TRUE ELSE FALSE END' ;
    }
}