0

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

我有几个模型:

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

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

https://github.com/jwg2s/temp

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

谢谢

4

1 回答 1

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));
}

我希望这能帮助你更多地理解可包含性。

于 2011-07-13T07:42:28.693 回答