【问题标题】:CakePHP Retrieve results from multiple tables in one modelCakePHP 在一个模型中从多个表中检索结果
【发布时间】:2013-02-12 07:47:11
【问题描述】:

CakePHP 版本:2.X

我已经完全编辑了这篇文章,以便将我的问题重新表述得更清楚。
我正在创建一个包含 5 个表格的专业提案系统:

提案.........> id - 名称 - 内容 - 创建
客户........> id - 名称 - 内容 - 创建 - proposal_id
产品............> id - 名称 - 内容 - 创建 - client_id
规格..> id - 名称 - 内容 - 创建 - product_id
附录.....> id - 名称 - 内容 - 已创建 - product_id

为了本示例的目的,我已经简化了列。

所以关联如下:

提案 > HasMany > 客户
客户 > BelongsTo > 提案/客户 > HasMany > 产品
产品 > BelongsTo > 客户/产品 > HasMany > 规格
产品 > BelongsTo > 客户/产品 > HasMany > 附录
规格 > BelongsTo > 产品 BelongsTo > 产品

然后我创建了 5 个具有上述关联的模型。

看起来像这样:

建议 1
....客户 1
........产品1
......规格1
......规格2
......规格3
......附录1
......附录2
........产品2
......规格4
......规格5
…………附录 3
…………附录 4

提案 2
....客户端 2
........产品3
......规格6
......规格7
......规格8
......附录5
…………附录 6
........产品 4
......规格9
......规格10
......附录7
…………附录 8

等等……

我的问题是如何获得一个看起来像这样的数组并在我的 ProposalController.php 操作的视图 index.ctp 中检索所有这些信息?

非常感谢您的帮助!

【问题讨论】:

  • 您应该认真阅读如何在控制台中烘焙。它将为您自动化模型中的所有关系。这有点过时但仍然有效.. 我推荐 youtube.com/playlist?list=PL9B2E2E37CCB661D6 Andrew Perkins 教程,主要是 Cakephp 博客教程第 10 部分 - 设置 Cake 控制台和烘焙
  • 谢谢 LetterSticker,有些日子我会尝试这样做,但目前我正在学习 OOP 和 CakePHP 框架,所以我仍然喜欢亲自动手,所以我知道它是如何工作的。我不得不承认这有点让人头疼,但我认为一旦你知道它是如何工作的,你就可以完全享受这个框架的全部力量。
  • 有人能引导我走向正确的方向吗,我真的被困在这里了吗?我不知道如何利用它们来显示一个数组,其中包含根据他们的链接表包含的所有信息。非常感谢您,我真的很感激。

标签: cakephp


【解决方案1】:

我终于成功地使用可包含行为构建了我想要的东西,但我遇到了一个我无法解决的问题。 我想检索提案 ID 并显示其附加的客户、产品、规格和附录。

为此,我在我的 ProposalsController.php 中进行了视图操作,如下所示:

function admin_view($id){
        $this->loadModel('Client');
        // $d = $this->Proposal->find('all', array(
        $d['proposals'] = $this->Proposal->find('all', array(
            'conditions' => array('Proposal.id' => $id),
            //'contain' => array() Tableau vide pour supprimer les liaisons
            'contain' => array('Client' => array(
                'fields' => array('id','name'),
                'Product' => array(
                    'Specification', 'fields'=>array('id','name'),
                    'Appendice', 'fields'=>array('id','name')
                    )
                ))
            ));
        $this->set($d);
        // debug($d);
    }

在我的情况下,我正在通过其 id 检索特定提案。然后我将 ma 查询发送到我的 admin_view.ctp:

<h1>VIEW</h1>

<?php
// debug($proposals);
?>

<p>
    Proposition : <strong><?php echo $proposals[0]['Proposal']['name']; ?></strong>
    pour le client <strong><?php echo $proposals[0]['Client']['name']; ?></strong>
    ayant le produit <strong><?php echo $proposals[0]['Client']['Product'][0]['name']; ?></strong>
    avec la spec <strong><?php echo $proposals[0]['Client']['Product'][0]['Specification'][0]['name']; ?></strong>
    et l'annexe <strong><?php echo $proposals[0]['Client']['Product'][0]['Appendice'][0]['name']; ?></strong>
</p>

它有效,但似乎很混乱,尤其是我必须使用 [0] 的方式,这在我看来是不够的。此外,如果提案没有任何产品、规格或附录,它会给我一个错误,因为它们当然不存在。

如何重新排列我的代码以简化我的视图以使其更有意义?

以下是我的控制器视图操作中未注释的调试($d):

array(

        (int) 0 => array(
            'Proposal' => array(
                'id' => '1',
                'name' => 'Proposal 1',
                'created' => '2013-02-15 00:00:00',
                'modified' => '2013-02-16 03:00:47',
                'due' => '2013-02-28 00:00:00',
                'content' => 'Terms and conditions of the proposal 1.'
            ),
            'Client' => array(
                'id' => '1',
                'name' => 'Client 1',
                'Product' => array(
                    (int) 0 => array(
                        'id' => '7',
                        'name' => 'produit 1',
                        'client_id' => '1',
                        'Specification' => array(
                            (int) 0 => array(
                                'id' => '8',
                                'name' => 'spec 1 produit 1',
                                'value' => 'value 1 produit 1',
                                'product_id' => '7'
                            ),
                            (int) 1 => array(
                                'id' => '9',
                                'name' => 'spec 2 produit 1',
                                'value' => 'value 2 produit 1',
                                'product_id' => '7'
                            )
                        ),
                        'Appendice' => array(
                            (int) 0 => array(
                                'id' => '12',
                                'name' => 'Annexe 1 produit 1',
                                'content' => 'content annexe 1 produit 1',
                                'product_id' => '7'
                            )
                        )
                    ),
                    (int) 1 => array(
                        'id' => '8',
                        'name' => 'produit 2',
                        'client_id' => '1',
                        'Specification' => array(
                            (int) 0 => array(
                                'id' => '10',
                                'name' => 'spec 1 produit 2',
                                'value' => 'value 1 produit 2',
                                'product_id' => '8'
                            ),
                            (int) 1 => array(
                                'id' => '11',
                                'name' => 'spec 2 produit 2',
                                'value' => 'value 2 produit 2',
                                'product_id' => '8'
                            ),
                            (int) 2 => array(
                                'id' => '12',
                                'name' => 'spec 3 produit 2',
                                'value' => 'value 3 produit 2',
                                'product_id' => '8'
                            )
                        ),
                        'Appendice' => array(
                            (int) 0 => array(
                                'id' => '13',
                                'name' => 'Annexe 1 produit 2',
                                'content' => 'content annexe 1 produit 2',
                                'product_id' => '8'
                            )
                        )
                    )
                )
            )
        ),
        (int) 1 => array(
            'Proposal' => array(
                'id' => '1',
                'name' => 'Proposal 1',
                'created' => '2013-02-15 00:00:00',
                'modified' => '2013-02-16 03:00:47',
                'due' => '2013-02-28 00:00:00',
                'content' => 'Terms and conditions of the proposal 1.'
            ),
            'Client' => array(
                'id' => '2',
                'name' => 'Client 2',
                'Product' => array()
            )
        )
    )

我得到了我需要的意思,即 id 为 1 的提案,但下面有另一个数组显示我不需要的 2 个模型提案和客户端的关联,因为我已经在第一个数组(int)0 中拥有它。

我做错了什么?

非常感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多