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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

how do I pull imagedataitems with category = whatever


Go to End


6 Posts   1169 Views

Avatar
zim

Community Member, 135 Posts

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

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

Avatar
bummzack

Community Member, 904 Posts

23 November 2009 at 7:48pm

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

Avatar
bummzack

Community Member, 904 Posts

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 %>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

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 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.

Avatar
zim

Community Member, 135 Posts

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.