简体   繁体   中英

Is possible to associate models with current time conditions?

Is possible to get two models associated with current time condition?

<?php

class SomeModel extends AppModel {

    public $hasOne = array(
        'ForumBan',
        'ForumBanActive' => array(
            'className' => 'ForumBan',
            'conditions' => array('ForumBanActive.end_time >' => time()) // fatal error
        ),
    );
}

I don't want to add this condition every time i call find on ForumBan model.

Basic lesson in php OOP: You can't call methods and functions in object property declarations. http://php.net/manual/en/language.oop5.properties.php

Set the association in the __construct() method of the model or use 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',
);

like i said, thanks @burzum for advice, but copy-past solution from my answer isn't cool, shame on you!

Following @burzum answer i got wanted result.

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',
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM