CakePHP 和 Containable

发布于 2024-11-19 21:21:34 字数 385 浏览 5 评论 0原文

首先我要说的是,我已经尝试尽可能多地阅读 CakePHP 书籍来了解这个特定主题,但无论出于何种原因,我就是无法弄清楚这一点。

我有几个模型:

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

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

https://github.com/jwg2s/temp

我要么在输出数组中获取太多数据,或者几乎没有..我只是无法弄清楚这个可控制的,如果我能看到我正在尝试做的事情的例子那就太好了...

谢谢

Let me start out by saying I've tried to read the CakePHP book as much as I can for this particular topic, and for whatever reason, I just cannot figure this out.

I have a few models:

Person

  • each person can have many jobs
  • each job has one branch and each branch belongsTo one city.

At first I was relying on recursive, but its not customizable enough (obviously) do get the data that I want.

https://github.com/jwg2s/temp

I either get WAY too much data in my output array, or hardly any at all.. I just can't figure this containable out, and if I could see an example of what I'm trying to do it would be great...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2024-11-26 21:21:35

既然您已经从头到尾阅读了这本食谱,我不会与您一起了解所有基本细节,而只是尝试解释如何使用该容器。

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

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

我希望这可以帮助您更多地了解可收容性。

since you've read the Cookbook up and down I won't go through all the basic details with you and just try to explain how to use the containable.

You can use the containable with the find-functions from your models. Either from your controller or directly from your model.
Most of the time I use the controller so I will give you an example how you would do it from there. I also try to use your specific example, so you have something to work with.

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

I hope this helped you in understanding containable a little more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文