开发者

Is there a way to define Model relationships with conditions in CakePHP?

I am using CakePHP 1.3.

I have a few questions regarding CakePHP Models:

1) Is there a way to define a Model with conditions, such that when invoked using the Containable, behavior, I do not need to define 'conditions' for it again. For example: I have two models - "Store" and "Deal" where "Store" that hasMany "Deal". I want to accomplish the following without having to define these conditions for "Deal" every time:

$this->Store('all', array('contain'=>array('Deal'=>array('isactive'=>1,'now() < expirydate', 'qty > 0')));

2) Also, is there a way to define relationships between Models differently based on conditions? In other words, how can I define "Store" hasMany "Deals", "ActiveDeal", "ExpiredDeal", etc.., all on the 'deals' table b开发者_如何学Cut differing based on conditions I set for each.

Much appreciate any help.

Thanks/Regards..


If you look at the first couple of code examples on the Associations: Linking Models Together page of the CakePHP Cookbook, you'll see that you can add conditions to your model joins that will work whenever you do a basic find.

Therefore you should be able to do something like the following:

class Store extends AppModel {
    var $name = 'Store';

    var $hasMany = array(
        'Deal' => array(
            'className' => 'Deal',
            'foreignKey' => 'store_id',
            'conditions' => array(
                'Deal.isactive' => '1',
                'now() < Deal.expirydate',
                'Deal.qty > 0'
            ),
            'order' => ''
        ),
        'ExpiredDeal' => array(
            'className' => 'Deal',
            'foreignKey' => 'store_id',
            'conditions' => array('now() >= ExpiredDeal.expirydate'),
            'order' => ''
        )
    );
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜