7919 Posts in 1358 Topics by 932 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 601 Views |
-
how do I pull imagedataitems with category = whatever

23 November 2009 at 12:37pm
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
-
Re: how do I pull imagedataitems with category = whatever

23 November 2009 at 1:34pm
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
-
Re: how do I pull imagedataitems with category = whatever

23 November 2009 at 7:48pm Last edited: 23 November 2009 7:57pm
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.
-
Re: how do I pull imagedataitems with category = whatever

23 November 2009 at 8:01pm
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 %> -
Re: how do I pull imagedataitems with category = whatever

24 November 2009 at 3:24am
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 urlAlso 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.
-
Re: how do I pull imagedataitems with category = whatever

24 November 2009 at 6:29am
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.
| 601 Views | ||
|
Page:
1
|
Go to Top |


