CakePHP 有一个效率低下的地方吗?

发布于 2024-08-30 00:01:49 字数 732 浏览 2 评论 0原文

我正在查看 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 技术交流群。

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

发布评论

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

评论(2

盛夏已如深秋| 2024-09-06 00:01:49

Model::find 使用联接来检索通过 hasOnebelongsTo 关系与源模型关联的模型记录。特定的 SQL 查询看起来像这样(使用 Car owningTo Driver 模型结构):

SELECT `Car`.`brand`, `Car`.`colour`, `Driver`.`name`, `Driver`.`age`
    FROM `cars` AS `Car`
    LEFT JOIN `drivers` AS `Driver` ON `Car`.`id` = `Driver`.`car_id`
WHERE `Car`.`brand` = 'Aston Martin'

属于每个模型的字段可以通过表/模型别名解析为相应的数组元素:

  • Car. brand 变为 $result['Car']['brand']
  • Car.colour 变为 $result['Car']['colour' ]
  • Driver.name 变为 $result['Driver']['name']
  • Driver.age 变为 $ result['Driver']['age']

只需一个查询。

检索 hasManyhasAndBelongsToMany 关联的数据需要额外的查询,有时甚至更多。

Model::find uses joins to retrieve model records associated to the source model by hasOne and belongsTo relationships. The specific SQL queries look something like (using a Car belongsTo Driver model structure):

SELECT `Car`.`brand`, `Car`.`colour`, `Driver`.`name`, `Driver`.`age`
    FROM `cars` AS `Car`
    LEFT JOIN `drivers` AS `Driver` ON `Car`.`id` = `Driver`.`car_id`
WHERE `Car`.`brand` = 'Aston Martin'

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 and hasAndBelongsToMany associations requires additional queries, sometimes many more.

野却迷人 2024-09-06 00:01:49

很确定大多数(如果不是全部)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

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