【问题标题】:CakePHP and ContainableCakePHP 和可容纳
【发布时间】:2011-07-13 00:10:26
【问题描述】:

首先让我说我已经尝试尽可能多地阅读 CakePHP 书籍以了解这个特定主题,但无论出于何种原因,我都无法理解这一点。

我有几个模型:

  • 每个人可以有很多工作
  • 每个职位都有一个分支,每个分支都属于一个城市。

起初我依赖递归,但它的可定制性不够(显然)确实得到了我想要的数据。

https://github.com/jwg2s/temp

我要么在我的输出数组中得到太多数据,要么几乎没有。太好了……

谢谢

【问题讨论】:

  • 您能否通过一些示例更具体一些,而不仅仅是链接到您的 Github 存储库?

标签: cakephp controller models containable


【解决方案1】:

既然你已经阅读了这本食谱,我不会和你一起讨论所有的基本细节,而只是尝试解释如何使用容器。

您可以使用模型中的 find-functions 来包含容器。来自您的控制器或直接来自您的模型。
大多数时候我都使用控制器,所以我会给你一个例子,你将如何从那里做到这一点。我也尝试使用您的具体示例,以便您可以使用。

/app/controllers/people_controller.php

function index() {
    //I like to write a separate array for fields and contain
    $fields = array(
        'name',
        'birthday',
        'gender'
    );

    /* It's important to know, that the fields will not get included into the
     * contain-array unless it's an associated model! */
    $contain = array(
        'Job' => array(
            //within another array you define the next level of contain
            'Branch' => array(
                //you get the deal...
                'City'
            ),
            //if you only need specific fields you can define this here like this:
            'fields' => array('title', 'date', 'salary'),
            //or order them directly!
            'order' => 'Job.salary DESC'
        )
    );

    //we now to our find-fall with our 2 arrays for the fields and the contain
    //every option (like fields or order) can be used in the containable
    $people = $this->Person->find('all', array('contain' => $contain, 'fields' => $fields));
}

我希望这能帮助您进一步了解可包含性。

【讨论】:

  • 谢谢,这绝对有助于解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多