简体   繁体   English

在magento中使用类似查询获取类别集合

[英]Get category collection with like query in magento

I want to show all category list in a autosuggest field. 我想在自动建议字段中显示所有类别列表。 So I have a code with like query as -- 所以我有一个带有类似查询的代码-

function res($cur_category){
        $children_categories = Mage::getModel('catalog/category')->getCategories($cur_category->getId());
    $children_categories->addAttributeToFilter('name', array('like' => '%a%'));  
        foreach($children_categories as $child){
             $name = $child->getName();
        $option.='<li onClick="fill(\''.addslashes($child->getName()).'\');">' .$child->getName(). "</li>";
        $option.=res($child);
        }
    unset($children_categories);
        return $option;
}

function GetTree(){
    $rootcatId= Mage::app()->getStore()->getRootCategoryId();
    $categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
    $categories->addAttributeToFilter('name', array('like' => '%a%')); 
    foreach($categories as $category) 
    {   
        $option.='<li onClick="fill(\''.addslashes($category->getName()).'\');">'.$category->getName()."</li>";
        if($category->hasChildren()){
        $option.= res($category);
        }
    }
    unset($categories);
    return $option;
} 
echo $select = '<ul>'.GetTree().'</ul>';

But this code is not working with like query, without like query it is working fine and show category list properly. 但是此代码无法与类似查询一起使用,如果没有类似查询,它就可以正常工作并正确显示类别列表。 Please provide me query with like. 请给我查询喜欢。

It may be due to the use of the "addAttributeToFilter" method, or using the "getCategories" to define the collection... My best suggestion is to change up the way you're pulling the collections. 可能是由于使用了“ addAttributeToFilter”方法,或者是使用了“ getCategories”来定义集合。我最好的建议是改变提取集合的方式。 I haven't tested this in a function, it's always been a direct .phtml file, but I always use the following structure to pull a collection (modified to fit your variables): 我尚未在函数中对此进行测试,它始终是直接的.phtml文件,但我始终使用以下结构来提取集合(已修改以适合您的变量):

$children_categories = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addFieldToFilter('parent_id',array('eq' => $cur_category->getId()))
                ->addFieldToFilter('name', array('like' => '%a%'))
                ->addFieldToFilter('include_in_menu',array('eq' => '1'))
                ->addFieldToFilter('is_active', array('eq' => '1'))
                ->addAttributeToSort('position', 'asc');

The last three methods are optional in your case, but I'll provide them here for those who want to retain only visible categories to be used in a menu, and sorted the same as your category tree. 后三种方法在您的情况下是可选的,但是我在这里为那些只希望保留菜单中使用的可见类别并与类别树进行排序的人提供它们。

And for the GetTree() method: 对于GetTree()方法:

$categories = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addFieldToFilter('parent_id',array('eq' => $rootcatId))
                ->addFieldToFilter('name', array('like' => '%a%'))
                ->addFieldToFilter('include_in_menu',array('eq' => '1'))
                ->addFieldToFilter('is_active', array('eq' => '1'))
                ->addAttributeToSort('position', 'asc');

Let me know if this helps, or you're still running into an issue. 让我知道这是否有帮助,或者您仍然遇到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM