可以将模型与当前时间条件相关联


Is possible to associate models with current time conditions?

是否有可能获得与当前时间条件相关的两个模型?

<?php
class SomeModel extends AppModel {
    public $hasOne = array(
        'ForumBan',
        'ForumBanActive' => array(
            'className' => 'ForumBan',
            'conditions' => array('ForumBanActive.end_time >' => time()) // fatal error
        ),
    );
}

我不想在每次调用ForumBan模型上的find时都添加此条件。

php OOP基础课:不能在对象属性声明中调用方法和函数。http://php.net/manual/en/language.oop5.properties.php

在模型的__construct()方法中设置关联,或者使用bindModel():

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    $this->hasOne['ForumBanActive']['conditions'] = array('ForumBanActive.end_time >' => time()));
}
public $hasOne = Array(
    'ForumBan',
    'ForumBanActive' => array('className' => 'ForumBan'),
    'UserFile',
    'BlogProfile',
);

就像我说的,谢谢@burzum的建议,但是从我的答案中复制过去的解决方案并不酷,为你感到羞耻!

在@burzum回答之后,我得到了想要的结果。

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    $this->hasOne['ForumBanActive']['conditions'] = array('ForumBanActive.end_time >' => time()));
}
public $hasOne = Array(
    'ForumBan',
    'ForumBanActive' => array('className' => 'ForumBan'),
    'UserFile',
    'BlogProfile',
);