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   2568 Views

Avatar
LesC

4 November 2009 at 12:57am Community Member, 70 Posts

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

19 November 2009 at 12:38pm (Last edited: 19 November 2009 12:38pm), Community Member, 17 Posts

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

19 November 2009 at 12:51pm Community Member, 17 Posts

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

19 November 2009 at 1:26pm Community Member, 244 Posts

I think that you would find range('a', 'z') useful here, rather than manually creating an array of letters.

Avatar
Chris Rae

19 November 2009 at 1:28pm Community Member, 17 Posts

Good call ;)

Replace the big ol' alphabetical list with this.

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

Avatar
UncleCheese

19 November 2009 at 3:45pm 4085 Posts

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

19 November 2009 at 4:09pm Community Member, 17 Posts

Your wish is my command.

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

Avatar
Juanitou

19 November 2009 at 11:17pm Community Member, 323 Posts

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

Best regards,
Juan