【问题标题】:CakePHP specify join condition hasOne/hasMany polymorphic associationCakePHP 指定连接条件 hasOne/hasMany 多态关联
【发布时间】:2015-10-13 03:50:22
【问题描述】:

我已经建立了一个多态关联,它是这样的:

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 子模型中,这样它就不会每次都查询所有子模型,而是查看类型然后选择正确的子模型?

【问题讨论】:

  • 答案似乎在问题中。你有没有尝试过?

标签: mysql cakephp


【解决方案1】:

穿着同样的鞋子。 您可以添加如下条件:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多