如何在模板中显示另一个数据对象

Kav*_*ana 1 php silverstripe

这是Area类的功能

        public function findLatestReport()
    {
        $getReport = Report::get()->filter('AreaID',$this->ID)
            ->sort('Date ASC')->first();
        return $getReport;
    }
Run Code Online (Sandbox Code Playgroud)

这是报告类功能

    public function getWeatherStatus()
    {
        return $this->Fields()->filter('Name', 'Weather Status')->first();
    }
Run Code Online (Sandbox Code Playgroud)

有什么方法可以显示WeatherStatus详细信息的“区域”模板。

<% loop $findLatestReport %>
        {$WeatherStatus}
<% end_loop %>
Run Code Online (Sandbox Code Playgroud)

小智 5

在返回单个DataObject(带有)而不是列表时with,您需要使用,而不是。然后,您应该可以访问:loop->first()$WeatherStatus

<% with $findLatestReport %>
    {$WeatherStatus}
<% end_with %>
Run Code Online (Sandbox Code Playgroud)