在 CakePHP 中使用递归模型属性
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在处理相关模型时,例如
modelA
和modelB
,如果您想将find()
与modelA
一起使用,您应该在该模型中设置recursive
属性,而不是在modelB
中。因此,在您的代码中,您应该这样设置:
您还可以使用
可包含的
行为或fields
选项find()
方法。When dealing with related models, say
modelA
andmodelB
, if you want to usefind()
withmodelA
, you should set therecursive
property in that model, not inmodelB
.Thus in your code you should set it like this:
You can also have more control over what is retrieved using the
Containable
behavior or thefields
option of thefind()
method.