سؤال

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?

هل كانت مفيدة؟

المحلول

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}

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top