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.

Data Model Questions /

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

has_many and template


Go to End


2 Posts   1667 Views

Avatar
alex_sm

Community Member, 19 Posts

29 June 2017 at 9:02pm

Edited: 29/06/2017 9:05pm

I have two classes : Letter and LetterFiles (attached files to the letter):

  
Class  Letter  extends  DataObject  {  
     private static $db = array (
    	 'Sender' => 'Text',
	 'SendTo' => 'Text',
……		
  );
  private static $has_many = array (
  	'Letters' => 'LetterFiles'  );
	);
……
}

Class  LetterFiles  extends  File {  
     private  static  $has_one = array (
  	'Files' => 'Letter'
  );
.........
  }

  

I want to show all letters and all attached files on LetterPage:

class  LetterPage  extends  Page {
	private $db = array();
}

class  LetterPage_Controller  extends Page_Controller {
       public function index () {
      $letters = Letter::get()->sort('DateUpload');
      return array (
			'AllLetters' => $letters
			);

}

I want to use template LetterPage.ss:

<% loop $AllLetters %>
            <tr>
                …….
                <td>$Sender</td>
                <td>$SendTo</td>    
                <td>$Theme</td>
                <td>
                      <% loop $Files %>
                          $Name
                      <% end_loop %>

                </td>
                
            </tr>                    
<% end_loop %>

But it doesn't render attached files.
Please, give me any ideas.

Avatar
alex_sm

Community Member, 19 Posts

5 July 2017 at 7:52pm