0

I'm writing my first extbase extension for TYPO3 with nested models at the moment.

Following models exists:

Author - attributes: name and description

News - attributes: title, date, author

The author is included in the news model like this

/**
 * @var Tx_Extbase_Persistence_ObjectStorage<Tx_Simplenews_Domain_Model_Author>
 * @lazy
 * @cascade remove
 **/
protected $author = 0;

Debugging in Fluid also works, but the author object has an key uuid ("000000007d9412bd000000000217f7d0" for example), which changes on every request.

I just want to show the author's name on every news. One name.

So i have to loop trough the author object, to find the key and display the name like this:

<f:for each="{oneNews.author}" as="author">
    <td>{author.name}</td>`
</f:for>

Is there a better solution for this?

<f:for each="{news}" as="oneNews">
    <td>{oneNews.author.name}</td>
</f:for>

won't work.

Thanks in advance!

Patrick Hafner
  • 129
  • 3
  • 12

3 Answers3

1

Got the answer

I just updated following code in News.php (Model):

/**
* @var Tx_Extbase_Persistence_ObjectStorage<Tx_Simplenews_Domain_Model_Author>
* @lazy
**/
protected $author;

Constructor:

public function __construct() {
    $this->author = new Tx_Extbase_Persistence_ObjectStorage();
}

Getter:

/**
 * @return Tx_Simplenews_Domain_Model_Author
 */
public function getAuthor() {
    $author = $this->author;
    $author->rewind(); // rewinds the iterator to the first storage element
    return $author->current(); // returns the current storage entry.
}

Now I can access the author's name with {oneNews.author.name}

Patrick Hafner
  • 129
  • 3
  • 12
1

So, why do you use an objectStorage for Author in the first place? ObjectStorages are for storing multiple Objects. So as long as a news of yours cant have two or more authors at the same time, you dont need an objectStorage at all for that property. Then you dont need to just return the first object of the objectStorage via your getAuthor() method. This, btw, makes the hole object Storage usage obsolete.

I assume a news only has one author. Try this:

News Model:

/**
 * @var Tx_Simplenews_Domain_Model_Author
 **/
protected $author;

/**
 * @param Tx_Simplenews_Domain_Model_Author $author
 * @return void
 */
public function setAuthor(Tx_Simplenews_Domain_Model_Author $author) {
  $this->author = $author;
}

/**
 * @return Tx_Simplenews_Domain_Model_Author
 */
public function getAuthor() {
  return $this->author;
}

In your Fluid-Template you still have:

{oneNews.author.name}

So, just dont use an objectStorage if you dont need it.

Daniel
  • 6,916
  • 2
  • 36
  • 47
  • Just have a related problem: In Fluid, i cannot access some parents (n:1) with {news.author.name}. Different objects have several n:1 relations (alle definded the same in ext_tables.sql, TCA and model). This only occures in some cases. – JKB Feb 02 '19 at 01:06
0

I created a dummy news extension which lists the news the way you want it. See this git repository. I don't know what went wrong in your case. BTW, I used the Extension Builder to create the extension and just changed the List.html template for the News.

Rico Leuthold
  • 1,975
  • 5
  • 34
  • 49
  • I tried to remove the `@lazy` annotation and $author without initialisation. But still can't access the author's object directly – Patrick Hafner Jul 02 '12 at 14:53
  • I just wanted to create an extension without the extension builder to understand extbase. Thanks for the dummy extension, i've got the answer now. Wait a few minutes – Patrick Hafner Jul 03 '12 at 12:43
  • IMHO the convention over configuration approach has a lot of advantages. However, careless mistakes can happen so easily. This is why I think the Extension Builder is a very good way to get started. You have a running code basis where you can apply your changes, and get more insight into Extbase / Fluid with every tweak. – Rico Leuthold Jul 04 '12 at 08:34