【问题标题】:Joining models in cake php在 cake php 中加入模型
【发布时间】:2011-12-21 08:43:15
【问题描述】:

我有这张 NPO 表,它有一个模板。模板又具有主题。我想检索Npo选择的主题。

我有以下关系设置:

NPO - npo.id 上的模板 = template.npoid 模板 - theme.id 上的主题 = template.template_theme_id

我正在使用:

$this->Npo->bindmodel(array(
  'hasOne' => array(
    'NpoTemplate' => array(
      'className' => 'NpoTemplate'
    )
  )
), false);

$this->NpoTemplate->bindmodel(array(
  'hasOne' => array(
    'TemplateTheme' => array(
      'className' => 'TemplateTheme',
      'fields' => 'TemplateTheme.html'
    )
  )
), false);

$arrUserSite = $this->Npo->find('first', array(
  'conditions'=> array(
    'Npo.address' => $user
  )
));

但它不会从 TemplateTheme 获取任何内容。相反,它为此表编写一个单独的查询,并且在连接中不考虑它。

我已将recursive 级别设置为3

请帮忙。我真的不明白蛋糕协会是如何运作的。

问候 希曼舒·夏尔马

【问题讨论】:

  • belogsTo:你的意思是……belongsTo

标签: cakephp join


【解决方案1】:

尝试在单个 bindModel() 调用中设置您的关系。

$this->Npo->bindmodel(array(
  'hasOne' => array(
    'NpoTemplate' => array(
      'foreignKey' => false,
      'conditions' => array(
        'Npo.id = NpoTemplate.npoid'
      )
    ),
    'TemplateTheme' => array(
      'foreignKey' => false,
      'conditions' => array(
        'NpoTemplate.template_theme_id = TemplateTheme.id'
      )
    )
  )
));


$arrUserSite = $this->Npo->find('first', array(
  'conditions' => array(
    'Npo.address' => $user 
  )
));

您可能需要调整 bindModel 设置,因为我不能 100% 确定您的数据库结构。祝你好运。

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多