在 CakePHP 中使用递归模型属性

发布于 2024-12-18 19:49:44 字数 1036 浏览 3 评论 0原文

我正在使用 CakePHP 2.0 和 MySQL。我有下表:

Stores
-id
-name
-suburb_id

Outlets
-id
-name
-suburb_id

Suburbs
-id
-name
-state_id

States
-id
-code
-name

州有许多郊区和郊区有许多商店。当我执行以下查找时:

$this->Outlet->recursive = 0;
$stores = $this->Store->find("all", array('limit' => 50));

它检索 Outlet、Suburb、Store 和 State 数据数组。

[Outlet] => Array
(
    [id] => 3
    [name] => SMOKEYS KEBABS PIZZA /PIDE
    [suburb_id] => 1212
)
[Suburb] => Array
(
    [id] => 1212
    [state_id] => 1
    [name] => Wiangaree
    [State] => Array
    (
        [id] => 1
        [code] => ACT
        [name] => Australian Capital Territory
    )
    [Store] => Array
    (
        [0] => Array
        (
            [id] => 12814
            [name] => Wiangaree General Store
            [suburb_id] => 1212
        )
    )
)

是否有办法控制它检索的内容?本质上,我想要奥特莱斯、相关的郊区数据和相关的州数据。

或者我处理这个问题的方式不正确?我想到的另一个选择是实施临时连接。

I'm using CakePHP 2.0 and MySQL. I have the following tables:

Stores
-id
-name
-suburb_id

Outlets
-id
-name
-suburb_id

Suburbs
-id
-name
-state_id

States
-id
-code
-name

States hasMany Suburbs and Suburbs hasMany stores. When I perform the following find:

$this->Outlet->recursive = 0;
$stores = $this->Store->find("all", array('limit' => 50));

It retrieves the Outlet, Suburb, Store and State data arrays.

[Outlet] => Array
(
    [id] => 3
    [name] => SMOKEYS KEBABS PIZZA /PIDE
    [suburb_id] => 1212
)
[Suburb] => Array
(
    [id] => 1212
    [state_id] => 1
    [name] => Wiangaree
    [State] => Array
    (
        [id] => 1
        [code] => ACT
        [name] => Australian Capital Territory
    )
    [Store] => Array
    (
        [0] => Array
        (
            [id] => 12814
            [name] => Wiangaree General Store
            [suburb_id] => 1212
        )
    )
)

Is there anyway to control what it retrieves? Essentially, I would like the Outlets, related Suburb data and related state data.

Or am I going about this incorrectly? The other option I thought of would be implemented ad hoc joins.

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

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

发布评论

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

评论(1

左岸枫 2024-12-25 19:49:44

在处理相关模型时,例如 modelAmodelB,如果您想将 find()modelA 一起使用,您应该在该模型中设置 recursive 属性,而不是在 modelB 中。

因此,在您的代码中,您应该这样设置:

$this->Store->recursive = 0;
$stores = $this->Store->find("all", array('limit' => 50));

您还可以使用 可包含的行为fields 选项 find()方法。

When dealing with related models, say modelA and modelB, if you want to use find() with modelA, you should set the recursive property in that model, not in modelB.

Thus in your code you should set it like this:

$this->Store->recursive = 0;
$stores = $this->Store->find("all", array('limit' => 50));

You can also have more control over what is retrieved using the Containable behavior or the fields option of the find()method.

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