Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Using ID in Controller

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

Page: 1
Go to End
Author Topic: 620 Views
  • SeanBoy
    Avatar
    Community Member
    8 Posts

    Using ID in Controller Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Using ID in Controller Link to this post

    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...

  • SeanBoy
    Avatar
    Community Member
    8 Posts

    Re: Using ID in Controller Link to this post

    Hi Martimiz

    Problem solved. I now understand my various errors.

    Many thanks for taking the time to explain.

    Sean

    620 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.