7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Alphabetical list of objects stored with DOM
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | Next > | |
| Author | Topic: | 2064 Views |
-
Re: Alphabetical list of objects stored with DOM

4 November 2009 at 12:57am
Thanks again UncleCheese, I managed to get it working, and it's sweet as a nut
Here's the code for anyone else that might need it:
private static $col1_alpha = array ('a','b','c','d','e','f','g','h','i','j','k','l','m');
private static $col2_alpha = array ('n','o','p','q','r','s','t','u','v','w','x','y','z');private function getGuideEntries($column)
{
$set = new DataObjectSet();
$letters = $column;
foreach($letters as $letter) {
$filter = "Title > '$letter'";
$filter .= ($next = next($letters)) ? " AND Title < '$next'" : "";
$set->push(new ArrayData(array(
'Letter' => $letter,
'Listings' => $this->ResourceItems($filter, "Title ASC")
))
);
}
return $set;
}public function getAtoMEntries()
{
return $this->getGuideEntries(self::$col1_alpha);
}public function getNtoZEntries()
{
return $this->getGuideEntries(self::$col2_alpha);
}Cheers
-
Re: Alphabetical list of objects stored with DOM

19 November 2009 at 12:38pm Last edited: 19 November 2009 12:38pm
Won't solutions like this return all A's and all B's under "A", and all B's and all C's under "B" etc?
Title < A AND Title > C includes all A's and all B's - not just all B's
-
Re: Alphabetical list of objects stored with DOM

19 November 2009 at 12:51pm
I would've gone with this:
function Letters() {
$set = new DataObjectSet();
$letters = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
foreach($letters as $letter) {
$filter = "Title Like '$letter%'";
$set->push(new ArrayData(array(
'Letter' => $letter,
'Listings' => $this->Films($filter,"Title ASC")
))
);
}
return $set;
} -
Re: Alphabetical list of objects stored with DOM

19 November 2009 at 1:26pm
I think that you would find range('a', 'z') useful here, rather than manually creating an array of letters.
-
Re: Alphabetical list of objects stored with DOM

19 November 2009 at 1:28pm
Good call ;)
Replace the big ol' alphabetical list with this.
$letters = range('A','Z');
-
Re: Alphabetical list of objects stored with DOM

19 November 2009 at 3:45pm
Wow. Nice function. I'll have to remember that one. PHP is full of surprises like that.
I think the only reason for the < > vs the LIKE operator was because his original spec was for everything before and after M, rather than blocked out by each letter. I like how this came out, though. Someone should make a recipe out of it.
-
Re: Alphabetical list of objects stored with DOM

19 November 2009 at 4:09pm
Your wish is my command.
http://doc.silverstripe.org/doku.php?id=recipes:alphabetical_dataobjectset
-
Re: Alphabetical list of objects stored with DOM

19 November 2009 at 11:17pm
Thanks! Summarising the thread in a recipe very nice of you.
Best regards,
Juan
| 2064 Views | ||
| Go to Top | Next > |



