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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Random image from gallery


Go to End


6 Posts   1208 Views

Avatar
tomjohn

Community Member, 19 Posts

14 August 2014 at 7:21am

I have Gallery and error 500 on page ;-/

My code is:

in page.php

class Page extends SiteTree {
private static $many_many = array (
'Gallery'	=>	'Image' 
);

 function getCMSFields() 
	{

   

		$fields = parent::getCMSFields();
		  if($this->ID) {
			$uploadField = new UploadField(
			        $name = 'Gallery',
			        $title = 'Photos:');
			$pathTo = 'article/' . $this->ID . '/gallery';
			$uploadField->setFolderName($pathTo);
$fields->addFieldToTab('Root.Gallery', $uploadField);
}			
      
		return $fields;

	}			
}
class Page_Controller extends ContentController {
.....
public function Randomlmage(){
     return Galeria::get()->sort('RAND()')->First(); 
   }

in page.ss
<% loop RandomImage %>
   <img src="$URL" alt="$Title"  /> 
         <% end_loop %> 

Anyone can help me? I don't have access to server error logs.

Avatar
Kirk

Community Member, 67 Posts

14 August 2014 at 11:16am

Is there a spelling mistake in the following code

return Galeria::get()->sort('RAND()')->First();

If that is not the issue first of all I would take out the RandomImage loop form Page.ss and if the error does not occur then at least it gives you an idea of where the error is happening, and I would then start returning early from RandomImage to pinpoint what line of code is throwing the error.

Avatar
tomjohn

Community Member, 19 Posts

15 August 2014 at 2:34am

Thanks for your response. My mistake when rewritting to this post. I used of course:

public function Randomlmage(){
return Gallery::get()->sort('RAND()')->First();
}

I enabled error reporting I have:

[User Error] Uncaught ReflectionException: Class Galeria does not exist
and
[User Error] DataList::create Can't find data classes (classes linked to tables) for Galeria. Please ensure you run dev/build after creating a new DataObject.

I made
class Gallery extends DataExtension {
private static $belongs_many_many = array(
'Page' => 'Page'
);

Still nothing. Finaly this code works for me:

 function RandomImage(){
            return $this->Gallery()->sort('RAND()')->First(); 
}

Only one problem is .. I can't make working this function on parent page.

<% loop $Children %> 
.....
<% loop RandomImage %>
   <img src="$URL" alt="$Title" title="ccc" /> 
         <% end_loop %> 
......
 <% end_loop %> 

Can you help me?

Avatar
Kirk

Community Member, 67 Posts

15 August 2014 at 8:48am

Edited: 15/08/2014 8:48am

Try putting a $Debug inside the loop and it will dump out all available variables and will help you with your troubleshooting

<% loop RandomImage %> 
$Debug
<% end_loop %>

Also I would make sure Gallery does has some rows as if it is empty it will never enter the loop.

Avatar
tomjohn

Community Member, 19 Posts

15 August 2014 at 9:39am

Thanks. You've got right. Debug show only:

    Name:Random
    Table:
    Value:

when I use random function on parent page. Its some strange. Take a look:

On parent, which show full content from all children (like a news page):

<% loop $Children %>
.....
<% loop RandomImage %>
<img src="$URL" alt="$Title" title="ccc" />
<% end_loop %>
......
<% end_loop %>

No images from child pages.

But code :

<% loop $Children %>
.....
<% loop Gallery %>
<img src="$URL" alt="$Title" title="ccc" />
<% end_loop %>
......
<% end_loop %>

show images.

On children page

<% loop RandomImage %>
<img src="$URL" alt="$Title" title="ccc" />
<% end_loop %>

show images too.

There are those same images for each time uploaded with multiupload field on child page. It's look like I can't access to proper child ID on parent page to get RandomImage to work.
<% loop Gallery %> is not enough for me, because I need random images and limit their number on page.

Avatar
Kirk

Community Member, 67 Posts

15 August 2014 at 11:18am

I would check the RandomImage method to confirm it is working as expected, also see if there are any fresh errors in the error log.
Maybe try some debug code like the example below

public function Randomlmage(){
//Debug::Dump(Gallery::get()->count()); exit; 
//return Gallery::get()->first(); 
//Debug::Dump($this->Gallery()->count()); exit; 
//return $this->Gallery()->first(); 
//return $this->Gallery()->get();
return Gallery::get(); 
}