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.

Data Model Questions /

Using ID in Controller


Reply


3 Posts   713 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, 1107 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