Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Alphabetical list of objects stored with DOM


Reply


17 Posts   2736 Views

Avatar
LesC

Community Member, 70 Posts

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

Avatar
Chris Rae

Community Member, 17 Posts

19 November 2009 at 12:38pm

Edited: 19/11/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

Avatar
Chris Rae

Community Member, 17 Posts

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;
   }

Avatar
ajshort

Community Member, 244 Posts

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.

Avatar
Chris Rae

Community Member, 17 Posts

19 November 2009 at 1:28pm

Good call ;)

Replace the big ol' alphabetical list with this.

$letters = range('A','Z');

Avatar
UncleCheese

Forum Moderator, 4096 Posts

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.

Avatar
Chris Rae

Community Member, 17 Posts

19 November 2009 at 4:09pm

Your wish is my command.

http://doc.silverstripe.org/doku.php?id=recipes:alphabetical_dataobjectset

Avatar
Juanitou

Community Member, 323 Posts

19 November 2009 at 11:17pm

Thanks! Summarising the thread in a recipe very nice of you.

Best regards,
Juan