Question

Let's say we have a 1:n relation between an Author and Book model. Modeling the relation using the Extension Builder, the necessary code to get Book's for an author is provided.

But what about the inverse? How do I get the Author for a given Book class?

Was it helpful?

Solution

I assume you have created the following domain model with the Extension Builder.

Domain model built with Extension Builder

When you create a 1:n relation between Author and Book, the resulting database table for Book will contain a field which holds the UID of the author. To make use of this field, you have to add a getter in your book domain model to return the corresponding author for the given book.

Add the following to your domain model for book:

/**
 * Returns the author
 *
 * @var \TYPO3\YourExtension\Domain\Model\Author
 */
protected $author;

/**
 * @return \TYPO3\YourExtension\Domain\Model\Author
 */
public function getAuthor() {
    return $this->author;
}

Now you can use the new getter in Fluid to return the author of a given book with {book.author}

OTHER TIPS

You can add n:1 relation to Book model, then Extension builder will create getter and setter automatically..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top