CakePHP指定连接条件hasOne / hasMany多态关联

时间:2015-10-13 03:50:23

标签: mysql cakephp

我有一个多态关联,我设置它是这样的:

Media has a 1-to-1 association with either Photo, Video or Text

媒体模型的摘录如下所示:

public $hasOne = array(
    'Photo' => array(
        'className' => 'Photo',
        'foreignKey' => 'media_id',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
    'Text' => array(
        'className' => 'Text',
        'foreignKey' => 'media_id',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),

有没有办法指定像

这样的条件
'conditions' => ['LfrMomentArticle.type' => 'photo'],

在Photo子模型中,以便每次都不会查询所有子模型,而是查看类型,然后选择正确的子模型?

1 个答案:

答案 0 :(得分:0)

走在同一双鞋子里。 你可以添加如下条件:

'conditions' => ['LfrMomentArticle.type' => 'photo'],

如果您的LfrMomentArticle模型中有一个类型字段(当然还有数据库中)。

相关问题