【问题标题】:How to stop Silverstripe SearchContext from throwing Version Table_Live error如何阻止 Silverstripe SearchContext 抛出 Version Table_Live 错误
【发布时间】:2013-04-04 20:58:11
【问题描述】:

考虑以下 Silverstripe 页面类

<?php

class Page extends SiteTree{

static $has_many = array('OtherDataObjects' => 'DataObjectClass');

public function getSearchContext() {

    $fields = new FieldSet(

        new TextField('Title', 'Tour'),
        new DropdownField('OtherDataObjects', 'Other Data Object', array('data', 'value')
    );

    $filters = array(
      'Title' => new PartialMatchFilter('Title'),
      'OtherDataObjects' => new PartialMatchFilter('OtherDataObjects.Title')
    );
    return new SearchContext(
      'Page', 
      $fields, 
      $filters
   );
  }
}

将此搜索表单添加到前端表单并发布搜索表单总是会导致 [用户错误] 并在末尾包含类似这样的 SQL 错误。

AND ("DataObjectClass_Live"."DataObjectClass_Live" LIKE 'title') ORDER BY "Sort" LIMIT 25 OFFSET 0 Table 'database DataObjectClass_Live' doesn't exist

每次我尝试对 has_many 关系运行搜索时,我的 searchcontext 搜索都会引发错误。版本化扩展似乎是罪魁祸首,因为无论基类是否具有版本化扩展,它都会向所有表添加 _live 我在 SilverStripe 版本 2.4.x 和最新的 3.0.x 版本中遇到相同的错误。

任何帮助或指点将不胜感激。

【问题讨论】:

  • 版本化的站点树扩展将 _live 添加到所有搜索查询中,这就是为什么它会抛出表不退出错误。如果数据对象没有版本化扩展,版本化需要注意不要将 _live 添加到 sql 查询中。

标签: search silverstripe


【解决方案1】:

也许可以尝试使用 sqlQuery。像

function SearchResults() {
  $select = array('*');
  $from = array('OtherDataObjects');
  $where = array('OtherDataObjects:PartialMatch' => '%' . $data['Title'] . '%');
  $sqlQuery = new SQLQuery($select, $from, $where);
  $results = $sqlQuery->execute();
  return $results;
}

$data['Title'] 是搜索文本框中的值

部分匹配参考:http://doc.silverstripe.org/framework/en/topics/datamodel

sql查询参考:http://doc.silverstripe.org/framework/en/reference/sqlquery

【讨论】:

  • 谢谢@Gavin。我最终使用了一个自定义 SQLQuery 来进行搜索,并且效果很好。感谢您的提示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 2018-12-10
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多