【问题标题】:How to get relational fields data in one single query如何在一个查询中获取关系字段数据
【发布时间】:2015-08-13 04:42:50
【问题描述】:

我想优化我的网站的速度并在一个查询中获取所有数据,包括我的关系(选择)字段数据。该怎么做?

低于我目前的发现:

我注意到在执行$pod->field('relational_field'); 时会运行一个额外的查询。

假设一个 pod 有一个 name 字段 和一个 categories 字段,这是 relational(选择):

// This code below will behave unexpected 
$pod = pods( 'pod', array(
   'where' => array(...),
   'select' => array('t.name', 'category.name AS category_name')
));

假设你创建了一个Pod item链接到两个类别,你会得到两个结果,因为select选项中有category.name AS category_name。如果您为 pod 分配了一个类别,您将得到一个结果。

结果如下:

array (size=2)
  0 => 
    object(stdClass)[2928]
      public 'name' => string 'My pod 1' (length=8)
      public 'category' => string 'Category 1' (length=10)
  1 => 
    object(stdClass)[2929]
      public 'name' => string 'My pod 1' (length=8)
      public 'category' => string 'Category 2' (length=10) // <-- The difference is only in category

我真正想要的是那个'category'键中的两个类别的数据作为一个数组。这可能吗?

(原发于Pods.io

【问题讨论】:

标签: wordpress podscms


【解决方案1】:

这很接近。 'select' 参数应该是一个字符串,就像您将提供给 SQL SELECT 子句一样,而不是字段数组。与 'where' 参数相同,因此:

$pod = pods( 'pod', array(
   'where' => 'where params as a string',
   'select' => 't.name,category.name AS category_name')
));

当您使用field()display() 检索数据时,请使用别名“category_name”而不是点表示法。

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2022-01-17
    • 2018-04-05
    • 1970-01-01
    • 2017-09-13
    相关资源
    最近更新 更多