CakePHP 有一个效率低下的地方吗?
我正在查看 CakePHP 网站上的示例,特别是用于链接模型的 hasOne。 http://book.cakephp.org/view/78/Associations-链接模型在一起
我的问题是,CakePHP 是否使用两个查询来构建使用 hasOne 链接的模型中返回的数据的数组结构?
摘自 CakePHP: //$this->User->find() 调用的示例结果。
Array
(
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[Profile] => Array
(
[id] => 12
[user_id] => 121
[skill] => Baking Cakes
[created] => 2007-05-01 10:31:01
)
)
希望这一切都有道理。
I was looking at examples on the CakePHP website, in particular hasOne used in linking models.
http://book.cakephp.org/view/78/Associations-Linking-Models-Together
My question is this, is CakePHP using two queries to build the array structure of data returned in a model that uses hasOne linkage?
Taken from CakePHP:
//Sample results from a $this->User->find() call.
Array
(
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[Profile] => Array
(
[id] => 12
[user_id] => 121
[skill] => Baking Cakes
[created] => 2007-05-01 10:31:01
)
)
Hope this all makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Model::find
使用联接来检索通过hasOne
和belongsTo
关系与源模型关联的模型记录。特定的 SQL 查询看起来像这样(使用Car owningTo Driver
模型结构):属于每个模型的字段可以通过表/模型别名解析为相应的数组元素:
Car. brand
变为$result['Car']['brand']
Car.colour
变为$result['Car']['colour' ]
Driver.name
变为$result['Driver']['name']
Driver.age
变为$ result['Driver']['age']
只需一个查询。
检索
hasMany
和hasAndBelongsToMany
关联的数据需要额外的查询,有时甚至更多。Model::find
uses joins to retrieve model records associated to the source model byhasOne
andbelongsTo
relationships. The specific SQL queries look something like (using aCar belongsTo Driver
model structure):The fields belonging to each model can be parsed out into corresponding array elements by the table/model alias:
Car.brand
becomes$result['Car']['brand']
Car.colour
becomes$result['Car']['colour']
Driver.name
becomes$result['Driver']['name']
Driver.age
becomes$result['Driver']['age']
Only one query required.
Retrieving data for
hasMany
andhasAndBelongsToMany
associations requires additional queries, sometimes many more.很确定大多数(如果不是全部)hasOne 和belongsTo 都是使用join 完成的。所以一个查询。您始终可以在 config.php 中将 debug 设置为 2 以查看 sql 查询
pretty sure most if not all hasOne and belongsTo are done using join. so one query. you can always set debug to 2 in config.php to see the sql query