Osclass插件在搜索时添加不需要的表

时间:2014-05-16 16:55:47

标签: osclass

我使用cars plugin并且我复制了插件以添加另一个插件,摩托车。两个插件工作得很好,但搜索时出现致命错误。不知何故,他们在搜索时添加了另一个不需要的表(我在调试插件时看到了表)。例如,当我搜索汽车类别时,查询添加摩托车表。我只是添加了汽车桌而不是摩托车的一张。否则当我在摩托车类别上搜索时,查询会添加汽车表格。这让我感到困惑。结果永远不会纠正。这是一个错误吗?我使用的是Osclass 3.3.2

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。可能是我的解决方案不干净但它适用于我。我必须在/ oc-includes / osclass / model /目录中编辑Search.php文件。我复制了两个函数,addTable和addConditions。但我添加了一个新参数,即类别ID。所以它会是:addTableNew($ tables,$ catID)和addConditionsNew($ conditions,$ catID)

public function addTableNew($tables,$catID)
    {
        if(is_array($tables)) {
            foreach($tables as $table) {
                $table = trim($table);
                if($table!='' && in_array($catID, $this->categories)) {
                    $this->tables[] = $table;
                }
            }
        } else {
            $tables = trim($tables);
            if($tables!='' && in_array($catID, $this->categories)) {
                $this->tables[] = $tables;
            }
        }
    }

public function addConditionsNew($conditions,$catID)
    {
        if(is_array($conditions)) {
            foreach($conditions as $condition) {
                $condition = trim($condition);
                if($condition!='') {
                    if(in_array($catID, $this->categories)) {
                        $this->conditions[] = $condition;
                    }
                }
            }
        } else {
            $conditions = trim($conditions);
            if($conditions!='') {
                if(in_array($catID, $this->categories)) {
                    $this->conditions[] = $conditions;
                }
            }
        }
    }

然后在每个插件目录的index.php文件中。我更改了cars_search_conditions函数和motorbikes_search_conditions函数。

function cars_search_conditions($params) {
// we need conditions and search tables (only if we're using our custom tables)
$cats = ModelCars::newInstance()->getCategoryId('Cars');    //-->search the category id in the database
$catID = $cats['fk_i_category_id'];
$has_conditions = false ;
foreach($params as $key => $value) {
    // we may want to have param-specific searches
    switch($key) {
        case 'make':
            if( is_numeric($value) ) {
                Search::newInstance()->addConditionsNew(sprintf("%st_item_car_attr.fk_i_make_id = %d", DB_TABLE_PREFIX, $value),$catID);
                $has_conditions = true;
            }
        break;
        //others search param
    }
}

// Only if we have some values at the params we add our table and link with the ID of the item.
if($has_conditions) {
    Search::newInstance()->addConditionsNew(sprintf("%st_item.pk_i_id = %st_item_car_attr.fk_i_item_id ", DB_TABLE_PREFIX, DB_TABLE_PREFIX),$catID);
    Search::newInstance()->addTableNew(sprintf("%st_item_car_attr", DB_TABLE_PREFIX),$catID);
}

}

function motorbikes_search_conditions($params) {
// we need conditions and search tables (only if we're using our custom tables)fk_i_category_id
$cats = ModelMotorBikes::newInstance()->getCategoryId('Motorbikes');
$catID = $cats['fk_i_category_id'];
$has_conditions = false ;
foreach($params as $key => $value) {
    // we may want to have param-specific searches
    switch($key) {
        case 'make':
            if( is_numeric($value) ) {
                Search::newInstance()->addConditionsNew(sprintf("%st_item_motorbike_attr.fk_i_motorbike_make_id = %d", DB_TABLE_PREFIX, $value),$catID);
                $has_conditions = true;
            }
        break;
        //others search param
    }
}

// Only if we have some values at the params we add our table and link with the ID of the item.
if($has_conditions) {
    Search::newInstance()->addConditionsNew(sprintf("%st_item.pk_i_id = %st_item_motorbike_attr.fk_i_motorbike_item_id ", DB_TABLE_PREFIX, DB_TABLE_PREFIX),$catID);
    Search::newInstance()->addTableNew(sprintf("%st_item_motorbike_attr", DB_TABLE_PREFIX),$catID);

}

}