【问题标题】:backend list: sort and filter by field in HasMany relation后端列表:按 HasMany 关系中的字段排序和过滤
【发布时间】:2017-01-23 15:18:28
【问题描述】:

后端列表:按 HasMany 关系中的字段排序和过滤

我有两个相关的模特与戏剧作品和他们的表演:

class Play extends Model
{
 ...
public $table = 'johar_theater_plays';
public $hasMany = ['playtimes' => 'johar\Theater\Models\PlayTime'];
}

class PlayTime extends Model
{
...
public $table = 'johar_theater_plays_times';
public $belongsTo = ['play' =>  ['johar\Theater\Models\Play']];
}

我将通过 play 表单中的关系管理器管理游戏时间。

要在play-list 中显示playtimes,我发现有两种可能性:

playtimes:
    relation: playtimes
    select: time
    type: myRenderFunction

playtimes:        
    type: myRenderFunction
    sortable: true

第一种类型使用$value 中的集合调用我的函数,第二种使用JSON 字符串。

在第二种类型中,我可以按播放时间排序 - 它可能只是对 JSON 字符串进行排序,但这没关系。

但是我发现没有办法创建过滤器范围来过滤掉今天之后没有表现的所有播放。 我试过了

scope:
  condition: playtimes.time > now()

恕我直言,它的所有可能变体。以及创建一个范围函数,我在其中修改了不同的 where 子句。 问题始终是 SQL 将 JOIN 放入 group_concat:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'time' in 'where clause' 
(SQL: select `johar_theater_plays`.*, 
    (select group_concat(time separator ', ') 
    from `johar_theater_plays_times` 
    where `johar_theater_plays_times`.`play_id` = `johar_theater_plays`.`id`)
    as `playtimes` 
from `johar_theater_plays` 
where `time` > 2016-12-12 00:00:00 
order by `playtimes` desc)" on line 662 of E:\..\Illuminate\Database\Connection.php

所以简短的问题是:Play HasMany Playtimes

【问题讨论】:

    标签: octobercms octobercms-backend


    【解决方案1】:

    我找到了解决方案。如果有人有一个更漂亮的,我会很高兴,但现在我将使用这个范围函数:

    public function scopeFutureonly($query)
    {
        $table = 'johar_theater_plays_times';
        if(!$this->is_scoped) {
            $query->join($table, 'johar_theater_plays.id', '=', $table . '.play_id');
            $query->groupBy($table . '.play_id');
            $query->where($table . '.time', '>', date("Y-m-d H:i:s"));
        }
        $this->is_scoped = true;
    }
    

    isScoped 是模型中的受保护变量,用于防止在多次调用作用域时多次添加查询。 (感谢planetadeleste here)。

    唯一的问题是,我在 SQL 中得到了两个 JOINS:一个在 concat 中,一个在常规中。我认为这只能通过完全手动创建 SQL 来规避,这会破坏 october 的目的....

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多