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.

Customising the CMS /

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

Storing and Displaying a Collection of Testimonials


Go to End


35 Posts   8503 Views

Avatar
juneallison

Community Member, 110 Posts

10 August 2011 at 5:33am

Swaiba - Finally getting around to this again. I just wanted to wanted to share a bit a code with you before I test it. For just setting up the ability to associate testimonial categories with a page, does this seem right:

change this:

<?php
class Category extends DataObject {
static $db = array(
'Title' => 'Text'
);

static $belongs_many_many = array(
'Testimonials' => 'Testimonial',
);

}
?>

to

<?php
class Category extends DataObject {
static $db = array(
'Title' => 'Text'
);

static $belongs_many_many = array(
'Testimonials' => 'Testimonial',
'Pages' => 'Page'
);

}
?>

AND

add to Page.php this:

static $many_many = array(
'Categories' => 'Category'
);

and this:

function getCMSFields() {
$fields = parent::getCMSFields();
$dos = DataObject::get('Category');
$map = $dos ? $map = $dos->toDropdownMap() : array();
$msf = new MultiSelectField("Categories",'Testimonial Categories', $map);
$fields->addFieldToTab('Root.Content.TestimonialCategories', $msf);

return $fields;
}

Thanks for taking a look at this!

Avatar
juneallison

Community Member, 110 Posts

10 August 2011 at 6:13am

Swaiba- Here are my ideas for outputting a single random testimonial. I think I'm fuzzier on this part.

In page.php add this to the page controller:

public function getTestimonials() {
return DataObject::get("Testimonial", "TestimonialCategories", "RAND()", null, "1");
}

Looking at the documentation here is what each piece stands for:
DataObject::get($obj, $filter, $sort, $join, $limit);

I think I have the last 3 right. And maybe the 1st two are correct because we are filtering the testimonial object based on the testimonial categories assocaited with the page?

Then for my template page.ss:

<% if TestimonialCategories %>
<% control getTestimonials %>
<div class="one-testimonial">
$Quote<br />
$Signature
</div>
<% end_control %>
<% else %>

<% end_if %>

... But do I need the control loop? I'm guessing if I display a random one on page load, then I don't. If I'm rotating through random testimonials while you are still on the page then I'm guessing I do. Of course this will also require a bit a javascript. Otherwise I will end up with a list of random testimonials?

Any feedback would be great! Just wanted to run this by you before altering my database/code.

Avatar
MarcusDalgren

Community Member, 288 Posts

10 August 2011 at 6:55am

The $filter needs to be a MySQL where-clause so I don't think that part is going to work. The last three look fine, however if you just want one you can use DataObject::get_one() instead and you'll get just one Testimonial back. If you want the $filter to work you're probably going to have to join some tables to get the data.

Avatar
juneallison

Community Member, 110 Posts

10 August 2011 at 9:22am

Smurkas - Thanks for your input! I may just do the random portion at the jquery level. Because I think the desired output is to have the testimonials rotate while you are on the page.

I don't know if you've read the entire thread, but does the code I've outlined seem right and could the function then be simplified to something like this?

public function getTestimonials() {
return DataObject::get("Testimonial", "TestimonialCategories");
}

With the control loop in the template I'll then get all of the relevant testimonials. And will then use jquery to fade in and out random testimonials.

Thanks for your help!

Avatar
juneallison

Community Member, 110 Posts

11 August 2011 at 7:17am

Ok, I have started testing my code. The first issue: On a page that uses the page template it shows the Testimonial Groups tab (in the admin). It then only shows the left side of the multi-select box. I'm able to select something and it looks like it stays selected but there is no right side to add it to.

The code I added to create the multi-select in page.php:

function getCMSFields() {
$fields = parent::getCMSFields();
$dos = DataObject::get('Category');
$map = $dos ? $map = $dos->toDropdownMap() : array();
$msf = new MultiSelectField("Categories",'Testimonial Groups', $map);
$fields->addFieldToTab('Root.Content.TestimonialGroups', $msf);

return $fields;
}

Any suggestions would be great. Thank you!

Avatar
juneallison

Community Member, 110 Posts

11 August 2011 at 9:40am

Ok, so it looks like not having the right side of the multiselect is not an issue. I can still select multiple categories and they are retained after I save. I can also put $Categories in my template file and it will print out the two selected categories.

So now I'm not sure how to print out the testimonials associated with the categories associated with the page. Like Smurkas said, I think I need a way to join to two tables. Any hints on how to do this would be great. :)

This gives me all the testimonials:

public function getTestimonials() {
return DataObject::get("Testimonial");
}

Something like this gives me an error:

public function getTestimonials() {
return DataObject::get("Testimonial","Categories");
}

Any suggestions would be great! Thanks!

Avatar
MarcusDalgren

Community Member, 288 Posts

11 August 2011 at 7:41pm

Well actually you can print that directly in the template.

In the control that's printing the categories you can put another control for the testimonials.
So something like

<% control Categories %>
<!-- Print stuff from categories here -->
<% control Testimonials %>
<!-- Print stuff from testimonials here -->
<% end_control %>
<% end_control %>

Avatar
juneallison

Community Member, 110 Posts

12 August 2011 at 4:03am

Excellent! Thank you!

... but this only outputs testimonials from the first category. And I can see by using $categories that this page does indeed have two categories linked to it.

any advice would great! in the mean time, I am going to see if I can't figure out why this happens.

Thanks!