Jump to:

7938 Posts in 1541 Topics by 945 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 2518 Views
  • LesC
    Avatar
    Community Member
    70 Posts

    Re: Alphabetical list of objects stored with DOM Link to this post

    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

  • Chris Rae
    Avatar
    Community Member
    17 Posts

    Re: Alphabetical list of objects stored with DOM Link to this post

    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

  • Chris Rae
    Avatar
    Community Member
    17 Posts

    Re: Alphabetical list of objects stored with DOM Link to this post

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

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Alphabetical list of objects stored with DOM Link to this post

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

  • Chris Rae
    Avatar
    Community Member
    17 Posts

    Re: Alphabetical list of objects stored with DOM Link to this post

    Good call ;)

    Replace the big ol' alphabetical list with this.

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Alphabetical list of objects stored with DOM Link to this post

    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.

  • Chris Rae
    Avatar
    Community Member
    17 Posts
  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Alphabetical list of objects stored with DOM Link to this post

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

    Best regards,
    Juan

    2518 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.