如何从多个表cakePHP 3创建搜索表单

时间:2017-08-21 23:02:48

标签: cakephp associations query-builder cakephp-3.x

在本期杂志中我涉及2个表:工作和领域。 我试图创建搜索元素,它可以在所有视图中使用,并包含按作业标题过滤行的文本字段或按区域过滤行的作业说明和删除列表,我希望它们一起工作。 所以我在JobsController上创建了搜索功能:

#!/bin/bash
TOMCAT=apache-tomcat-7.0.23
TOMCAT_WEBAPPS=$TOMCAT/webapps
TOMCAT_CONFIG=$TOMCAT/conf/server.xml
TOMCAT_START=$TOMCAT/bin/startup.sh
TOMCAT_ARCHIVE=$TOMCAT.tar.gz
TOMCAT_URL=http://apache.mirrorcatalogs.com/tomcat/tomcat-7/v7.0.23/bin/$TOMCAT_ARCHIVE
WAR_FILE=whatever.war

if [ ! -e $TOMCAT ]; then
if [ ! -r $TOMCAT_ARCHIVE ]; then
if [ -n "$(which curl)" ]; then
    curl -O $TOMCAT_URL
elif [ -n "$(which wget)" ]; then
    wget $TOMCAT_URL
fi
fi

if [ ! -r $TOMCAT_ARCHIVE ]; then
echo "Tomcat could not be downloaded." 1>&2
echo "Verify that eiter curl or wget is installed." 1>&2
echo "If they are, check your internet connection and try again." 1>&2
echo "You may also download $TOMCAT_ARCHIVE and place it in this folder."  1>&2
exit 1
fi

tar -zxf $TOMCAT_ARCHIVE
rm $TOMCAT_ARCHIVE
fi

if [ ! -w $TOMCAT -o ! -w $TOMCAT_WEBAPPS ]; then
echo "$TOMCAT and $TOMCAT_WEBAPPS must be writable." 1>&2
exit 1
fi

if [ ! -r $WAR_FILE ]; then
echo "$WAR_FILE is missing. Download it and run this again to deploy it." 1>&2
else
cp $WAR_FILE $TOMCAT_WEBAPPS
fi

如果我通过工作接近区域

,它会很有效
public function search()
{
    if($this->request->data('area_select') != 'select area') {
        $jobs = $this->Jobs
            ->find('all')
            ->contain(['Types', 'Categories' => function($q) {
                return $q
                    ->where([
                        'Jobs.title OR Jobs.description LIKE' =>
                            "%" . $this->request->data('keywords') . "%"
                    ])
                    ->where([
                        'Jobs.area LIKE' =>
                            "%" . $this->request->data('area_select') ."%"
                    ]);
                }]
            );
    } else {
        $jobs = $this->Jobs
            ->find('all')
            ->contain(['Categories' => function($q) {
                return $q->where([
                    'Jobs.title OR Jobs.description LIKE' =>
                        "%" . $this->request->data('keywords') . "%"
                ]);
            }]
        );
    }

    $this->set('jobs',$jobs);
}   

问题在于,通过访问槽作业表,它会重复相同的区域,就像它只出现在作业表中一样多次... 所以,如果我试图像这样直接访问:

<?php foreach($jobs as $job): ?>
    <option><?php echo $job->area->name; ?></option>
<?php endforeach; ?>

它不起作用,因为我已提交给作业控制器。 那我怎么能克服这个问题?

0 个答案:

没有答案