Silverstripe自定义搜索URL,用于传递其所有参数

时间:2018-05-14 08:46:29

标签: php search filter orm silverstripe

它返回单个结果,更多组合不返回结果。

执行搜索我得到单个字段的结果,而不是为两个字段都设置值。我怎样才能获得两者?

我的错误是什么?谢谢你的帮助

在搜索表单中,我正在使用SliderField

class HomePage_Controller extends Page_Controller {

//...

public function index(SS_HTTPRequest $request) 
{  
        $properties = Solutions::get();

        if($visibility = $request->getVar('Visibility')) 
        {

            $properties = $properties->filter(array(
                'Solution:ExactMatch' => array('Visibility'),
                'Score:LessThanOrEqual' => $visibility
            ));
        }

        if($traffic = $request->getVar('Traffic')) 
        {

            $properties = $properties->filter(array(
                'Solution:ExactMatch' => array('Traffic'),
                'Score:LessThanOrEqual' => $traffic
            ));
        }

        return array 
        (
            'Results' => $properties->sort(array('Score' => 'DESC', 'Solution' => 'ASC'))
        );
}

public function SolutionForm() 
{

    $form = Form::create(
        $this,
        __FUNCTION__,
        FieldList::create(
            SliderField::create('Visibility','', 0, 100)                 
                ->addExtraClass('range-slider__range')
                ->setAttribute('type', 'range')
                ->setAttribute('min', '0')
                ->setAttribute('max', '100')
                ->setAttribute('step', '25')
                ->setValue('25'),
            SliderField::create('Traffic','', 0, 100)                 
                ->addExtraClass('range-slider__range')
                ->setAttribute('type', 'range')
                ->setAttribute('min', '0')
                ->setAttribute('max', '100')
                ->setAttribute('step', '25')
                ->setValue('0')

        ),
        FieldList::create(
            FormAction::create('doSolutionsSearch','')
                ->setUseButtonTag(true)
                ->setTitle('YOUR SOLUTION')
                ->addExtraClass('button-solution')  
        )
    );

    $form->setFormMethod('GET')
     ->setFormAction($this->Link('#home'))
     ->disableSecurityToken()
     ->loadDataFrom($this->request->getVars());

    return $form;
}}

1 个答案:

答案 0 :(得分:0)

那是因为如果两者都是真的,那么你的查询将如下所示:

WHERE ("Solutions"."Solution" = 'Visibility')
AND ("Solutions"."Solution" = 'Traffic')

这不会返回任何内容,因为Solution不会是可见性流量。

最简单的解决方案是执行两个查询并将它们合并在一起:

$properties = Solutions::get();
$arrayList = ArrayList::create();

if($visibilityScore = $request->getVar('Visibility')) {
    $arrayList->merge(
        $properties->filter(array(
            'Solution:ExactMatch' => 'Visibility',
            'Score:LessThanOrEqual' => $visibilityScore
        ))->toArray()
    );
}

if($trafficScore = $request->getVar('Traffic')) {
    $arrayList->merge(
        $properties->filter(array(
            'Solution:ExactMatch' => 'Traffic',
            'Score:LessThanOrEqual' => $trafficScore
        ))->toArray()
    );
}

return array(
    'Results' => $arrayList->sort(array(
        'Score' => 'DESC',
        'Solution' => 'ASC'
    ))
);