如何选择包含关联的字段? [英] How to select fields of a contained association?

查看:205
本文介绍了如何选择包含关联的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行以下查询,其中我只想从关联中读取必要的字段。

我在 select()中使用了一个点符号下面更好地解释我想要什么。

基本上 select()似乎关心用户。是否可以指定 Sites

的字段

  $ orders = $ this-> Orders-> find()
- > contains([
'Sites.Users'=> function($ q){
return $ q
- > select([
'Sites.id',
'Sites.user_id',
'Users.id',
'Users.name',
'Users.owner_id',
'Users.firstname',
'Users.lastname'
])
- > autoFields(false);
},
])
- > first();


解决方案

您必须单独配置容器,其他包含将不起作用。



确切地说,不能从相应的包含配置之外的任何地方选择包​​含的字段,唯一的例外是 belongsTo / hasOne 使用连接策略的关联,可以在 select()调用立即数父查询,因为这些关联将通过该查询中的联接检索。



如果 Sites 将是 belongsTo 关联,那么您可以通过 select()调用订单。如果 Users 将是 belongsTo ,但 Sites code> hasMany ,则可以使用 select()调用 Sites Users 选择字段。



话虽如此,在你的情况下你要么使用 queryBuilder 选项定义嵌套数组结构中的回调

  'sites'=> [
'queryBuilder'=> function($ q){
return $ q
- > select([
'Sites.id',
'Sites.user_id'
]);
},
'Users'=> function($ q){
return $ q
- " select([
'Users.id',
'Users.name',
'Users.owner_id',
'Users.firstname',
' Users.lastname'
]);
}
]
])


b $ b

或者,如果您实际上不需要查询构建器,请使用字段选项

  contains([
'Sites'=> [
'fields'=> [
'Sites.id',
'Sites.user_id'
],
'Users'=> [
'fields'=> [
'Users.id',
'Users.name',
'Users.owner_id',
'Users.firstname',
'Users.lastname'
]
]
]
])

另请参阅




  • Cookbok>数据库访问& ORM>查询生成器>将条件传递给包含

  • API> \Cake\ORM\QueryBuilder :: contain()


    • I would like to execute the following query where I'd like to read only the necessary fields from associated.
      I used a dot notation in select() below to better explain what I want.
      Basically the select() seems to concern Users only. Is it possible to specify the fields of Sites?

      $orders = $this->Orders->find()
          ->contain([
              'Sites.Users'=> function ($q) {
                  return $q
                      ->select([
                          'Sites.id',
                          'Sites.user_id',
                          'Users.id',
                          'Users.name',
                          'Users.owner_id',
                          'Users.firstname',
                          'Users.lastname'
                      ])
                      ->autoFields(false);
              },
          ])
          ->first();
      

      解决方案

      You have to configure the containments individually, selecting fields for other containments won't work.

      To be exact, you cannot select fields for a containment from anywhere else than the corresponding containment configuration, with the only exception of belongsTo/hasOne associations that are using the join strategy, fields for them can be selected in the select() call of the immediate "parent" query, as these associations are going to be retrieved via a join in that query.

      If for example Sites would be a belongsTo association, then you could select the fields for it via the select() call on Orders. If Users would be a belongsTo, but Sites a hasMany, then you could use the select() call for Sites to select fields for Users.

      That being said, in your case you either use the queryBuilder option to define callbacks in a nested array structure

      contain([
          'Sites' => [
              'queryBuilder' => function ($q) {
                  return $q
                      ->select([
                          'Sites.id',
                          'Sites.user_id'
                      ]);
              },
              'Users' => function ($q) {
                  return $q
                      ->select([
                          'Users.id',
                          'Users.name',
                          'Users.owner_id',
                          'Users.firstname',
                          'Users.lastname'
                      ]);
              }
          ]
      ])
      

      or, if you don't actually need the query builder, use the fields option

      contain([
          'Sites' => [
              'fields' => [
                  'Sites.id',
                  'Sites.user_id'
              ],
              'Users' => [
                  'fields' => [
                      'Users.id',
                      'Users.name',
                      'Users.owner_id',
                      'Users.firstname',
                      'Users.lastname'
                  ]
              ]
          ]
      ])
      

      See also

      这篇关于如何选择包含关联的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆