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

Using ID in Controller


Go to End


3 Posts   1307 Views

Avatar
SeanBoy

Community Member, 8 Posts

11 January 2011 at 11:14am

Hi

I am looking for help in a small issue I have, so all help would be greatly appreciated.

Within my template I have a control inside another control:

<% control Author %>
   $author_name
   <% control Top.Book %>
      $book_name<br/>
   <% end_control %>
<% end_control %>

Within my controller I have the following two functions:

public function getAuthor() {
    $authorObjects = DataObject::get("author");
    return $authorObjects;
  }

  public function getBook() {
    $bookObjects = DataObject::get("book", "authorsID={?????}");
    return $bookObjects;
  }

The problem is, I cannot work out how to get the the current author id within the getBook function. See ?????.

Does this make sense. If not, I'll try and explain better.

Many thanks in advance.

Sean

Avatar
martimiz

Forum Moderator, 1391 Posts

12 January 2011 at 9:22am

Edited: 12/01/2011 9:23am

The simplest would be something like this, where you don't need your Page_Controller functions (unless you wish to sort/filter your DataObjects some way):

class Author extends DataObject {
	static $has_many = array('Books' => 'Book');
	...
}

class Book extends DataObject {
	static $has_one = array('Author' => 'Author');
	...
}

<% control Author %>
	<p>Name: $Name</p>
	<% control Books %>
		<p>Book title: $Name</p>
	<% end_control %>

<% end_control %>

Hope this is what you were looking for...

Avatar
SeanBoy

Community Member, 8 Posts

12 January 2011 at 11:17am

Hi Martimiz

Problem solved. I now understand my various errors.

Many thanks for taking the time to explain.

Sean