Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » how do I pull imagedataitems with category = whatever

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 694 Views
  • zim
    Avatar
    Community Member
    135 Posts

    how do I pull imagedataitems with category = whatever Link to this post

    I am using imagedataobject like this.

    class Client extends DataObject
    {
       static $db = array (
          'Name' => 'Text',
          'Description' => 'Text',
          'Category' => "Enum('Direct, Agency')"
          
       );
       
       static $has_one = array (
          'Attachment' => 'Image',
          'ClientHolder' => 'ClientHolder'
       );
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Name'),
             new TextareaField('Description'),
             new DropdownField('Category','Category', singleton('Client')->dbObject('Category')->enumValues()),
             new FileIFrameField('Attachment')
          );
       }
    }

    all i need to know is how do i produce to the page items that have been saved as 'category=Direct', or 'Category=Agency'

    at the moment I am using

    <div class="client_wrapper">
    <% control Attachment %>
    <div class="client_main">
    $CroppedImage(167,113)
    </div>
    <% end_control %>
    </div>      
    <% end_control %>

    which produces all of them

    thanks

  • UncleCheese
    Avatar
    4085 Posts

    Re: how do I pull imagedataitems with category = whatever Link to this post

    Use an action in your URL..

       function category()
       {
          $clients = $this->Clients("Category = '".$this->urlParams['ID']."'");
          return $this->customise(array(
             'Clients' => $clients
          ))->renderWith(array('ClientHolder','Page'));
       }

    Assuming your holder page has the relation "Clients"..

    then you just need to use the url to filter by category..

    /my-staff-holder-page/category/foo

  • banal
    Avatar
    Community Member
    901 Posts

    Re: how do I pull imagedataitems with category = whatever Link to this post

    The method proposed by UncleCheese contains a potential security risk. You should never trust URL parameters without checking them first. Eg. something like this:

    function category()
    {
       $id = strtolower($this->urlParams['ID']);
       if($id != 'direct' && $id != 'agency')
          return array();
       
       $clients = $this->Clients("LOWER(Category) = '".$id."'");
       return $this->customise(array(
          'Clients' => $clients
       ))->renderWith(array('ClientHolder','Page'));
    }

    Edit Sorry, I accidentally posted this before I finished my post. The above method is probably the most basic of all, which checks if the ID is either 'direct' or 'agency'. A pattern match would also be fine:

    if( !preg_match('/(direct|agency)/i', $this->urlParams['ID']))
       ... get me out of here ...

    The advantage of the regex is, that it will match 'direct', 'Direct', 'DIRECT', etc. Make sure to normalize the value to lowercase, since otherwise you might not get any results, since the DB usually compares strings case-sensitive.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: how do I pull imagedataitems with category = whatever Link to this post

    If you just want to output some items at some part in your template, use something like this:

    function ClientsByCategory($category)
    {
       // get a set of clients, filtered by category
       return $this->Clients("Category = '".$category."'");
    }

    Then in your template use:

    <% control ClientsByCategory(Agency) %>
    ... all Agency Clients here
    <% end_control %>

    <% control ClientsByCategory(Direct) %>
    ... all Direct Clients here
    <% end_control %>

  • UncleCheese
    Avatar
    4085 Posts

    Re: how do I pull imagedataitems with category = whatever Link to this post

    This is why it's bad to filter by string and not a true data relation. Ideally, your categories are an object associated with your Client object, and then it just beccomes /category/123

    if($cat = DataObject::get_by_id("Category",$this->urlParams['ID']))
    // ok
    else
    // bad url

    Also helps to put an is_numeric check on there, too, so if some bonehead goes to /category/foo it doesn't throw a fatal error.

  • zim
    Avatar
    Community Member
    135 Posts

    Re: how do I pull imagedataitems with category = whatever Link to this post

    Thanks for help.

    I have used this:

    function ClientsByCategory($category)
    {
    // get a set of clients, filtered by category
    return $this->Clients("Category = '".$category."'");
    }

    Then in your template use:

    <% control ClientsByCategory(Agency) %>
    ... all Agency Clients here
    <% end_control %>

    <% control ClientsByCategory(Direct) %>
    ... all Direct Clients here
    <% end_control %>

    and has worked fine.

    694 Views
Page: 1
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.