I have a custom attribute called collection
(in retrospect, a horrible name all things considered,) with a custom attribute source model. The important part is here:
public function getAllOptions() {
if (is_null($this->_options)) {
$this->_options = array();
$rootCatId = Mage::app()->getStore(1)->getRootCategoryId();
$rootCat = Mage::getModel('catalog/category')->load($rootCatId);
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('level', array('eq'=>3))
->addFieldToFilter('parent_id', array('in'=>explode(',', $rootCat->getChildren())));
foreach ($categories as $category) {
/** @var Mage_Catalog_Model_Category $category */
$children = $category->getChildrenCategories();
if ($children) {
$subOptionArray = array();
foreach ($children as $child) {
$subOptionArray[ $child->getId() ] = array(
'label' => trim($child->getName()),
'value' => $child->getId()
);
}
if (!empty($subOptionArray)) {
$this->_options[ $category->getId() ] = array(
'label' => trim($category->getName()),
'value' => $subOptionArray
);
}
}
}
}
return $this->_options;
}
This gets a list of all level 4 categories, and lists them in <optgroup>'s
under their parent category's name. Like so:
A
1 => A1
2 => A2
3 => A3
B
4 => B1
5 => B2
6 => B3
This works great in the Admin when selecting values for this attribute. The problem comes with sorting a collection by this attribute's value.
I'm using the following snippet to try and sort by collection
:
->addAttributeToSelect('*')
->addAttributeToSort('collection', 'asc')
The problem is that this ends up sorting by the attribute's value, and not it's label. (The value here being the category IDs.) I need to be able to sort by the attribute's label, without having to convert the collection to an array and usort, or some post-load reflection sorting.
Any ideas what I'm missing? I feel like I'm missing something simple.